hso.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *
  4. * Driver for Option High Speed Mobile Devices.
  5. *
  6. * Copyright (C) 2008 Option International
  7. * Filip Aben <[email protected]>
  8. * Denis Joseph Barrow <[email protected]>
  9. * Jan Dumon <[email protected]>
  10. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  11. * <[email protected]>
  12. * Copyright (C) 2008 Greg Kroah-Hartman <[email protected]>
  13. * Copyright (C) 2008 Novell, Inc.
  14. *
  15. *****************************************************************************/
  16. /******************************************************************************
  17. *
  18. * Description of the device:
  19. *
  20. * Interface 0: Contains the IP network interface on the bulk end points.
  21. * The multiplexed serial ports are using the interrupt and
  22. * control endpoints.
  23. * Interrupt contains a bitmap telling which multiplexed
  24. * serialport needs servicing.
  25. *
  26. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  27. * port is opened, as this have a huge impact on the network port
  28. * throughput.
  29. *
  30. * Interface 2: Standard modem interface - circuit switched interface, this
  31. * can be used to make a standard ppp connection however it
  32. * should not be used in conjunction with the IP network interface
  33. * enabled for USB performance reasons i.e. if using this set
  34. * ideally disable_net=1.
  35. *
  36. *****************************************************************************/
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/sched/signal.h>
  39. #include <linux/slab.h>
  40. #include <linux/init.h>
  41. #include <linux/delay.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/module.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/usb.h>
  46. #include <linux/tty.h>
  47. #include <linux/tty_driver.h>
  48. #include <linux/tty_flip.h>
  49. #include <linux/kmod.h>
  50. #include <linux/rfkill.h>
  51. #include <linux/ip.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/usb/cdc.h>
  54. #include <net/arp.h>
  55. #include <asm/byteorder.h>
  56. #include <linux/serial_core.h>
  57. #include <linux/serial.h>
  58. #define MOD_AUTHOR "Option Wireless"
  59. #define MOD_DESCRIPTION "USB High Speed Option driver"
  60. #define HSO_MAX_NET_DEVICES 10
  61. #define HSO__MAX_MTU 2048
  62. #define DEFAULT_MTU 1500
  63. #define DEFAULT_MRU 1500
  64. #define CTRL_URB_RX_SIZE 1024
  65. #define CTRL_URB_TX_SIZE 64
  66. #define BULK_URB_RX_SIZE 4096
  67. #define BULK_URB_TX_SIZE 8192
  68. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  69. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  70. #define MUX_BULK_RX_BUF_COUNT 4
  71. #define USB_TYPE_OPTION_VENDOR 0x20
  72. /* These definitions are used with the struct hso_net flags element */
  73. /* - use *_bit operations on it. (bit indices not values.) */
  74. #define HSO_NET_RUNNING 0
  75. #define HSO_NET_TX_TIMEOUT (HZ*10)
  76. #define HSO_SERIAL_MAGIC 0x48534f31
  77. /* Number of ttys to handle */
  78. #define HSO_SERIAL_TTY_MINORS 256
  79. #define MAX_RX_URBS 2
  80. /*****************************************************************************/
  81. /* Debugging functions */
  82. /*****************************************************************************/
  83. #define hso_dbg(lvl, fmt, ...) \
  84. do { \
  85. if ((lvl) & debug) \
  86. pr_info("[%d:%s] " fmt, \
  87. __LINE__, __func__, ##__VA_ARGS__); \
  88. } while (0)
  89. /*****************************************************************************/
  90. /* Enumerators */
  91. /*****************************************************************************/
  92. enum pkt_parse_state {
  93. WAIT_IP,
  94. WAIT_DATA,
  95. WAIT_SYNC
  96. };
  97. /*****************************************************************************/
  98. /* Structs */
  99. /*****************************************************************************/
  100. struct hso_shared_int {
  101. struct usb_endpoint_descriptor *intr_endp;
  102. void *shared_intr_buf;
  103. struct urb *shared_intr_urb;
  104. struct usb_device *usb;
  105. int use_count;
  106. int ref_count;
  107. struct mutex shared_int_lock;
  108. };
  109. struct hso_net {
  110. struct hso_device *parent;
  111. struct net_device *net;
  112. struct rfkill *rfkill;
  113. char name[24];
  114. struct usb_endpoint_descriptor *in_endp;
  115. struct usb_endpoint_descriptor *out_endp;
  116. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  117. struct urb *mux_bulk_tx_urb;
  118. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  119. void *mux_bulk_tx_buf;
  120. struct sk_buff *skb_rx_buf;
  121. struct sk_buff *skb_tx_buf;
  122. enum pkt_parse_state rx_parse_state;
  123. spinlock_t net_lock;
  124. unsigned short rx_buf_size;
  125. unsigned short rx_buf_missing;
  126. struct iphdr rx_ip_hdr;
  127. unsigned long flags;
  128. };
  129. enum rx_ctrl_state{
  130. RX_IDLE,
  131. RX_SENT,
  132. RX_PENDING
  133. };
  134. #define BM_REQUEST_TYPE (0xa1)
  135. #define B_NOTIFICATION (0x20)
  136. #define W_VALUE (0x0)
  137. #define W_LENGTH (0x2)
  138. #define B_OVERRUN (0x1<<6)
  139. #define B_PARITY (0x1<<5)
  140. #define B_FRAMING (0x1<<4)
  141. #define B_RING_SIGNAL (0x1<<3)
  142. #define B_BREAK (0x1<<2)
  143. #define B_TX_CARRIER (0x1<<1)
  144. #define B_RX_CARRIER (0x1<<0)
  145. struct hso_serial_state_notification {
  146. u8 bmRequestType;
  147. u8 bNotification;
  148. u16 wValue;
  149. u16 wIndex;
  150. u16 wLength;
  151. u16 UART_state_bitmap;
  152. } __packed;
  153. struct hso_tiocmget {
  154. struct mutex mutex;
  155. wait_queue_head_t waitq;
  156. int intr_completed;
  157. struct usb_endpoint_descriptor *endp;
  158. struct urb *urb;
  159. struct hso_serial_state_notification *serial_state_notification;
  160. u16 prev_UART_state_bitmap;
  161. struct uart_icount icount;
  162. };
  163. struct hso_serial {
  164. struct hso_device *parent;
  165. int magic;
  166. u8 minor;
  167. struct hso_shared_int *shared_int;
  168. /* rx/tx urb could be either a bulk urb or a control urb depending
  169. on which serial port it is used on. */
  170. struct urb *rx_urb[MAX_RX_URBS];
  171. u8 num_rx_urbs;
  172. u8 *rx_data[MAX_RX_URBS];
  173. u16 rx_data_length; /* should contain allocated length */
  174. struct urb *tx_urb;
  175. u8 *tx_data;
  176. u8 *tx_buffer;
  177. u16 tx_data_length; /* should contain allocated length */
  178. u16 tx_data_count;
  179. u16 tx_buffer_count;
  180. struct usb_ctrlrequest ctrl_req_tx;
  181. struct usb_ctrlrequest ctrl_req_rx;
  182. struct usb_endpoint_descriptor *in_endp;
  183. struct usb_endpoint_descriptor *out_endp;
  184. enum rx_ctrl_state rx_state;
  185. u8 rts_state;
  186. u8 dtr_state;
  187. unsigned tx_urb_used:1;
  188. struct tty_port port;
  189. /* from usb_serial_port */
  190. spinlock_t serial_lock;
  191. int (*write_data) (struct hso_serial *serial);
  192. struct hso_tiocmget *tiocmget;
  193. /* Hacks required to get flow control
  194. * working on the serial receive buffers
  195. * so as not to drop characters on the floor.
  196. */
  197. int curr_rx_urb_idx;
  198. u8 rx_urb_filled[MAX_RX_URBS];
  199. struct tasklet_struct unthrottle_tasklet;
  200. };
  201. struct hso_device {
  202. union {
  203. struct hso_serial *dev_serial;
  204. struct hso_net *dev_net;
  205. } port_data;
  206. u32 port_spec;
  207. u8 is_active;
  208. u8 usb_gone;
  209. struct work_struct async_get_intf;
  210. struct work_struct async_put_intf;
  211. struct usb_device *usb;
  212. struct usb_interface *interface;
  213. struct device *dev;
  214. struct kref ref;
  215. struct mutex mutex;
  216. };
  217. /* Type of interface */
  218. #define HSO_INTF_MASK 0xFF00
  219. #define HSO_INTF_MUX 0x0100
  220. #define HSO_INTF_BULK 0x0200
  221. /* Type of port */
  222. #define HSO_PORT_MASK 0xFF
  223. #define HSO_PORT_NO_PORT 0x0
  224. #define HSO_PORT_CONTROL 0x1
  225. #define HSO_PORT_APP 0x2
  226. #define HSO_PORT_GPS 0x3
  227. #define HSO_PORT_PCSC 0x4
  228. #define HSO_PORT_APP2 0x5
  229. #define HSO_PORT_GPS_CONTROL 0x6
  230. #define HSO_PORT_MSD 0x7
  231. #define HSO_PORT_VOICE 0x8
  232. #define HSO_PORT_DIAG2 0x9
  233. #define HSO_PORT_DIAG 0x10
  234. #define HSO_PORT_MODEM 0x11
  235. #define HSO_PORT_NETWORK 0x12
  236. /* Additional device info */
  237. #define HSO_INFO_MASK 0xFF000000
  238. #define HSO_INFO_CRC_BUG 0x01000000
  239. /*****************************************************************************/
  240. /* Prototypes */
  241. /*****************************************************************************/
  242. /* Serial driver functions */
  243. static int hso_serial_tiocmset(struct tty_struct *tty,
  244. unsigned int set, unsigned int clear);
  245. static void ctrl_callback(struct urb *urb);
  246. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  247. static void hso_kick_transmit(struct hso_serial *serial);
  248. /* Helper functions */
  249. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  250. struct usb_device *usb, gfp_t gfp);
  251. static void handle_usb_error(int status, const char *function,
  252. struct hso_device *hso_dev);
  253. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  254. int type, int dir);
  255. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  256. static void hso_free_interface(struct usb_interface *intf);
  257. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  258. static int hso_stop_serial_device(struct hso_device *hso_dev);
  259. static int hso_start_net_device(struct hso_device *hso_dev);
  260. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  261. static int hso_stop_net_device(struct hso_device *hso_dev);
  262. static void hso_serial_ref_free(struct kref *ref);
  263. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  264. static int hso_mux_serial_read(struct hso_serial *serial);
  265. static void async_get_intf(struct work_struct *data);
  266. static void async_put_intf(struct work_struct *data);
  267. static int hso_put_activity(struct hso_device *hso_dev);
  268. static int hso_get_activity(struct hso_device *hso_dev);
  269. static void tiocmget_intr_callback(struct urb *urb);
  270. /*****************************************************************************/
  271. /* Helping functions */
  272. /*****************************************************************************/
  273. /* #define DEBUG */
  274. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  275. {
  276. return hso_dev->port_data.dev_net;
  277. }
  278. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  279. {
  280. return hso_dev->port_data.dev_serial;
  281. }
  282. /* Debugging functions */
  283. #ifdef DEBUG
  284. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  285. unsigned int len)
  286. {
  287. static char name[255];
  288. sprintf(name, "hso[%d:%s]", line_count, func_name);
  289. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  290. }
  291. #define DUMP(buf_, len_) \
  292. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  293. #define DUMP1(buf_, len_) \
  294. do { \
  295. if (0x01 & debug) \
  296. DUMP(buf_, len_); \
  297. } while (0)
  298. #else
  299. #define DUMP(buf_, len_)
  300. #define DUMP1(buf_, len_)
  301. #endif
  302. /* module parameters */
  303. static int debug;
  304. static int tty_major;
  305. static int disable_net;
  306. /* driver info */
  307. static const char driver_name[] = "hso";
  308. static const char tty_filename[] = "ttyHS";
  309. static const char *version = __FILE__ ": " MOD_AUTHOR;
  310. /* the usb driver itself (registered in hso_init) */
  311. static struct usb_driver hso_driver;
  312. /* serial structures */
  313. static struct tty_driver *tty_drv;
  314. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  315. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  316. static DEFINE_SPINLOCK(serial_table_lock);
  317. static const s32 default_port_spec[] = {
  318. HSO_INTF_MUX | HSO_PORT_NETWORK,
  319. HSO_INTF_BULK | HSO_PORT_DIAG,
  320. HSO_INTF_BULK | HSO_PORT_MODEM,
  321. 0
  322. };
  323. static const s32 icon321_port_spec[] = {
  324. HSO_INTF_MUX | HSO_PORT_NETWORK,
  325. HSO_INTF_BULK | HSO_PORT_DIAG2,
  326. HSO_INTF_BULK | HSO_PORT_MODEM,
  327. HSO_INTF_BULK | HSO_PORT_DIAG,
  328. 0
  329. };
  330. #define default_port_device(vendor, product) \
  331. USB_DEVICE(vendor, product), \
  332. .driver_info = (kernel_ulong_t)default_port_spec
  333. #define icon321_port_device(vendor, product) \
  334. USB_DEVICE(vendor, product), \
  335. .driver_info = (kernel_ulong_t)icon321_port_spec
  336. /* list of devices we support */
  337. static const struct usb_device_id hso_ids[] = {
  338. {default_port_device(0x0af0, 0x6711)},
  339. {default_port_device(0x0af0, 0x6731)},
  340. {default_port_device(0x0af0, 0x6751)},
  341. {default_port_device(0x0af0, 0x6771)},
  342. {default_port_device(0x0af0, 0x6791)},
  343. {default_port_device(0x0af0, 0x6811)},
  344. {default_port_device(0x0af0, 0x6911)},
  345. {default_port_device(0x0af0, 0x6951)},
  346. {default_port_device(0x0af0, 0x6971)},
  347. {default_port_device(0x0af0, 0x7011)},
  348. {default_port_device(0x0af0, 0x7031)},
  349. {default_port_device(0x0af0, 0x7051)},
  350. {default_port_device(0x0af0, 0x7071)},
  351. {default_port_device(0x0af0, 0x7111)},
  352. {default_port_device(0x0af0, 0x7211)},
  353. {default_port_device(0x0af0, 0x7251)},
  354. {default_port_device(0x0af0, 0x7271)},
  355. {default_port_device(0x0af0, 0x7311)},
  356. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  357. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  358. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  359. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  360. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  361. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  362. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  363. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  364. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  365. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  366. {USB_DEVICE(0x0af0, 0x7701)},
  367. {USB_DEVICE(0x0af0, 0x7706)},
  368. {USB_DEVICE(0x0af0, 0x7801)},
  369. {USB_DEVICE(0x0af0, 0x7901)},
  370. {USB_DEVICE(0x0af0, 0x7A01)},
  371. {USB_DEVICE(0x0af0, 0x7A05)},
  372. {USB_DEVICE(0x0af0, 0x8200)},
  373. {USB_DEVICE(0x0af0, 0x8201)},
  374. {USB_DEVICE(0x0af0, 0x8300)},
  375. {USB_DEVICE(0x0af0, 0x8302)},
  376. {USB_DEVICE(0x0af0, 0x8304)},
  377. {USB_DEVICE(0x0af0, 0x8400)},
  378. {USB_DEVICE(0x0af0, 0x8600)},
  379. {USB_DEVICE(0x0af0, 0x8800)},
  380. {USB_DEVICE(0x0af0, 0x8900)},
  381. {USB_DEVICE(0x0af0, 0x9000)},
  382. {USB_DEVICE(0x0af0, 0x9200)}, /* Option GTM671WFS */
  383. {USB_DEVICE(0x0af0, 0xd035)},
  384. {USB_DEVICE(0x0af0, 0xd055)},
  385. {USB_DEVICE(0x0af0, 0xd155)},
  386. {USB_DEVICE(0x0af0, 0xd255)},
  387. {USB_DEVICE(0x0af0, 0xd057)},
  388. {USB_DEVICE(0x0af0, 0xd157)},
  389. {USB_DEVICE(0x0af0, 0xd257)},
  390. {USB_DEVICE(0x0af0, 0xd357)},
  391. {USB_DEVICE(0x0af0, 0xd058)},
  392. {USB_DEVICE(0x0af0, 0xc100)},
  393. {}
  394. };
  395. MODULE_DEVICE_TABLE(usb, hso_ids);
  396. /* Sysfs attribute */
  397. static ssize_t hsotype_show(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. struct hso_device *hso_dev = dev_get_drvdata(dev);
  401. char *port_name;
  402. if (!hso_dev)
  403. return 0;
  404. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  405. case HSO_PORT_CONTROL:
  406. port_name = "Control";
  407. break;
  408. case HSO_PORT_APP:
  409. port_name = "Application";
  410. break;
  411. case HSO_PORT_APP2:
  412. port_name = "Application2";
  413. break;
  414. case HSO_PORT_GPS:
  415. port_name = "GPS";
  416. break;
  417. case HSO_PORT_GPS_CONTROL:
  418. port_name = "GPS Control";
  419. break;
  420. case HSO_PORT_PCSC:
  421. port_name = "PCSC";
  422. break;
  423. case HSO_PORT_DIAG:
  424. port_name = "Diagnostic";
  425. break;
  426. case HSO_PORT_DIAG2:
  427. port_name = "Diagnostic2";
  428. break;
  429. case HSO_PORT_MODEM:
  430. port_name = "Modem";
  431. break;
  432. case HSO_PORT_NETWORK:
  433. port_name = "Network";
  434. break;
  435. default:
  436. port_name = "Unknown";
  437. break;
  438. }
  439. return sprintf(buf, "%s\n", port_name);
  440. }
  441. static DEVICE_ATTR_RO(hsotype);
  442. static struct attribute *hso_serial_dev_attrs[] = {
  443. &dev_attr_hsotype.attr,
  444. NULL
  445. };
  446. ATTRIBUTE_GROUPS(hso_serial_dev);
  447. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  448. {
  449. int idx;
  450. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  451. if (serial->rx_urb[idx] == urb)
  452. return idx;
  453. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  454. return -1;
  455. }
  456. /* converts mux value to a port spec value */
  457. static u32 hso_mux_to_port(int mux)
  458. {
  459. u32 result;
  460. switch (mux) {
  461. case 0x1:
  462. result = HSO_PORT_CONTROL;
  463. break;
  464. case 0x2:
  465. result = HSO_PORT_APP;
  466. break;
  467. case 0x4:
  468. result = HSO_PORT_PCSC;
  469. break;
  470. case 0x8:
  471. result = HSO_PORT_GPS;
  472. break;
  473. case 0x10:
  474. result = HSO_PORT_APP2;
  475. break;
  476. default:
  477. result = HSO_PORT_NO_PORT;
  478. }
  479. return result;
  480. }
  481. /* converts port spec value to a mux value */
  482. static u32 hso_port_to_mux(int port)
  483. {
  484. u32 result;
  485. switch (port & HSO_PORT_MASK) {
  486. case HSO_PORT_CONTROL:
  487. result = 0x0;
  488. break;
  489. case HSO_PORT_APP:
  490. result = 0x1;
  491. break;
  492. case HSO_PORT_PCSC:
  493. result = 0x2;
  494. break;
  495. case HSO_PORT_GPS:
  496. result = 0x3;
  497. break;
  498. case HSO_PORT_APP2:
  499. result = 0x4;
  500. break;
  501. default:
  502. result = 0x0;
  503. }
  504. return result;
  505. }
  506. static struct hso_serial *get_serial_by_shared_int_and_type(
  507. struct hso_shared_int *shared_int,
  508. int mux)
  509. {
  510. int i, port;
  511. port = hso_mux_to_port(mux);
  512. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  513. if (serial_table[i] &&
  514. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  515. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  516. return dev2ser(serial_table[i]);
  517. }
  518. }
  519. return NULL;
  520. }
  521. static struct hso_serial *get_serial_by_index(unsigned index)
  522. {
  523. struct hso_serial *serial = NULL;
  524. unsigned long flags;
  525. spin_lock_irqsave(&serial_table_lock, flags);
  526. if (serial_table[index])
  527. serial = dev2ser(serial_table[index]);
  528. spin_unlock_irqrestore(&serial_table_lock, flags);
  529. return serial;
  530. }
  531. static int obtain_minor(struct hso_serial *serial)
  532. {
  533. int index;
  534. unsigned long flags;
  535. spin_lock_irqsave(&serial_table_lock, flags);
  536. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  537. if (serial_table[index] == NULL) {
  538. serial_table[index] = serial->parent;
  539. serial->minor = index;
  540. spin_unlock_irqrestore(&serial_table_lock, flags);
  541. return 0;
  542. }
  543. }
  544. spin_unlock_irqrestore(&serial_table_lock, flags);
  545. pr_err("%s: no free serial devices in table\n", __func__);
  546. return -1;
  547. }
  548. static void release_minor(struct hso_serial *serial)
  549. {
  550. unsigned long flags;
  551. spin_lock_irqsave(&serial_table_lock, flags);
  552. serial_table[serial->minor] = NULL;
  553. spin_unlock_irqrestore(&serial_table_lock, flags);
  554. }
  555. static void handle_usb_error(int status, const char *function,
  556. struct hso_device *hso_dev)
  557. {
  558. char *explanation;
  559. switch (status) {
  560. case -ENODEV:
  561. explanation = "no device";
  562. break;
  563. case -ENOENT:
  564. explanation = "endpoint not enabled";
  565. break;
  566. case -EPIPE:
  567. explanation = "endpoint stalled";
  568. break;
  569. case -ENOSPC:
  570. explanation = "not enough bandwidth";
  571. break;
  572. case -ESHUTDOWN:
  573. explanation = "device disabled";
  574. break;
  575. case -EHOSTUNREACH:
  576. explanation = "device suspended";
  577. break;
  578. case -EINVAL:
  579. case -EAGAIN:
  580. case -EFBIG:
  581. case -EMSGSIZE:
  582. explanation = "internal error";
  583. break;
  584. case -EILSEQ:
  585. case -EPROTO:
  586. case -ETIME:
  587. case -ETIMEDOUT:
  588. explanation = "protocol error";
  589. if (hso_dev)
  590. usb_queue_reset_device(hso_dev->interface);
  591. break;
  592. default:
  593. explanation = "unknown status";
  594. break;
  595. }
  596. /* log a meaningful explanation of an USB status */
  597. hso_dbg(0x1, "%s: received USB status - %s (%d)\n",
  598. function, explanation, status);
  599. }
  600. /* Network interface functions */
  601. /* called when net interface is brought up by ifconfig */
  602. static int hso_net_open(struct net_device *net)
  603. {
  604. struct hso_net *odev = netdev_priv(net);
  605. unsigned long flags = 0;
  606. if (!odev) {
  607. dev_err(&net->dev, "No net device !\n");
  608. return -ENODEV;
  609. }
  610. odev->skb_tx_buf = NULL;
  611. /* setup environment */
  612. spin_lock_irqsave(&odev->net_lock, flags);
  613. odev->rx_parse_state = WAIT_IP;
  614. odev->rx_buf_size = 0;
  615. odev->rx_buf_missing = sizeof(struct iphdr);
  616. spin_unlock_irqrestore(&odev->net_lock, flags);
  617. /* We are up and running. */
  618. set_bit(HSO_NET_RUNNING, &odev->flags);
  619. hso_start_net_device(odev->parent);
  620. /* Tell the kernel we are ready to start receiving from it */
  621. netif_start_queue(net);
  622. return 0;
  623. }
  624. /* called when interface is brought down by ifconfig */
  625. static int hso_net_close(struct net_device *net)
  626. {
  627. struct hso_net *odev = netdev_priv(net);
  628. /* we don't need the queue anymore */
  629. netif_stop_queue(net);
  630. /* no longer running */
  631. clear_bit(HSO_NET_RUNNING, &odev->flags);
  632. hso_stop_net_device(odev->parent);
  633. /* done */
  634. return 0;
  635. }
  636. /* USB tells is xmit done, we should start the netqueue again */
  637. static void write_bulk_callback(struct urb *urb)
  638. {
  639. struct hso_net *odev = urb->context;
  640. int status = urb->status;
  641. /* Sanity check */
  642. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  643. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  644. return;
  645. }
  646. /* Do we still have a valid kernel network device? */
  647. if (!netif_device_present(odev->net)) {
  648. dev_err(&urb->dev->dev, "%s: net device not present\n",
  649. __func__);
  650. return;
  651. }
  652. /* log status, but don't act on it, we don't need to resubmit anything
  653. * anyhow */
  654. if (status)
  655. handle_usb_error(status, __func__, odev->parent);
  656. hso_put_activity(odev->parent);
  657. /* Tell the network interface we are ready for another frame */
  658. netif_wake_queue(odev->net);
  659. }
  660. /* called by kernel when we need to transmit a packet */
  661. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  662. struct net_device *net)
  663. {
  664. struct hso_net *odev = netdev_priv(net);
  665. int result;
  666. /* Tell the kernel, "No more frames 'til we are done with this one." */
  667. netif_stop_queue(net);
  668. if (hso_get_activity(odev->parent) == -EAGAIN) {
  669. odev->skb_tx_buf = skb;
  670. return NETDEV_TX_OK;
  671. }
  672. /* log if asked */
  673. DUMP1(skb->data, skb->len);
  674. /* Copy it from kernel memory to OUR memory */
  675. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  676. hso_dbg(0x1, "len: %d/%d\n", skb->len, MUX_BULK_TX_BUF_SIZE);
  677. /* Fill in the URB for shipping it out. */
  678. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  679. odev->parent->usb,
  680. usb_sndbulkpipe(odev->parent->usb,
  681. odev->out_endp->
  682. bEndpointAddress & 0x7F),
  683. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  684. odev);
  685. /* Deal with the Zero Length packet problem, I hope */
  686. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  687. /* Send the URB on its merry way. */
  688. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  689. if (result) {
  690. dev_warn(&odev->parent->interface->dev,
  691. "failed mux_bulk_tx_urb %d\n", result);
  692. net->stats.tx_errors++;
  693. netif_start_queue(net);
  694. } else {
  695. net->stats.tx_packets++;
  696. net->stats.tx_bytes += skb->len;
  697. }
  698. dev_kfree_skb(skb);
  699. /* we're done */
  700. return NETDEV_TX_OK;
  701. }
  702. static const struct ethtool_ops ops = {
  703. .get_link = ethtool_op_get_link
  704. };
  705. /* called when a packet did not ack after watchdogtimeout */
  706. static void hso_net_tx_timeout(struct net_device *net, unsigned int txqueue)
  707. {
  708. struct hso_net *odev = netdev_priv(net);
  709. if (!odev)
  710. return;
  711. /* Tell syslog we are hosed. */
  712. dev_warn(&net->dev, "Tx timed out.\n");
  713. /* Tear the waiting frame off the list */
  714. if (odev->mux_bulk_tx_urb)
  715. usb_unlink_urb(odev->mux_bulk_tx_urb);
  716. /* Update statistics */
  717. net->stats.tx_errors++;
  718. }
  719. /* make a real packet from the received USB buffer */
  720. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  721. unsigned int count, unsigned char is_eop)
  722. {
  723. unsigned short temp_bytes;
  724. unsigned short buffer_offset = 0;
  725. unsigned short frame_len;
  726. /* log if needed */
  727. hso_dbg(0x1, "Rx %d bytes\n", count);
  728. DUMP(ip_pkt, min(128, (int)count));
  729. while (count) {
  730. switch (odev->rx_parse_state) {
  731. case WAIT_IP:
  732. /* waiting for IP header. */
  733. /* wanted bytes - size of ip header */
  734. temp_bytes =
  735. (count <
  736. odev->rx_buf_missing) ? count : odev->
  737. rx_buf_missing;
  738. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  739. odev->rx_buf_size, ip_pkt + buffer_offset,
  740. temp_bytes);
  741. odev->rx_buf_size += temp_bytes;
  742. buffer_offset += temp_bytes;
  743. odev->rx_buf_missing -= temp_bytes;
  744. count -= temp_bytes;
  745. if (!odev->rx_buf_missing) {
  746. /* header is complete allocate an sk_buffer and
  747. * continue to WAIT_DATA */
  748. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  749. if ((frame_len > DEFAULT_MRU) ||
  750. (frame_len < sizeof(struct iphdr))) {
  751. dev_err(&odev->net->dev,
  752. "Invalid frame (%d) length\n",
  753. frame_len);
  754. odev->rx_parse_state = WAIT_SYNC;
  755. continue;
  756. }
  757. /* Allocate an sk_buff */
  758. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  759. frame_len);
  760. if (!odev->skb_rx_buf) {
  761. /* We got no receive buffer. */
  762. hso_dbg(0x1, "could not allocate memory\n");
  763. odev->rx_parse_state = WAIT_SYNC;
  764. continue;
  765. }
  766. /* Copy what we got so far. make room for iphdr
  767. * after tail. */
  768. skb_put_data(odev->skb_rx_buf,
  769. (char *)&(odev->rx_ip_hdr),
  770. sizeof(struct iphdr));
  771. /* ETH_HLEN */
  772. odev->rx_buf_size = sizeof(struct iphdr);
  773. /* Filip actually use .tot_len */
  774. odev->rx_buf_missing =
  775. frame_len - sizeof(struct iphdr);
  776. odev->rx_parse_state = WAIT_DATA;
  777. }
  778. break;
  779. case WAIT_DATA:
  780. temp_bytes = (count < odev->rx_buf_missing)
  781. ? count : odev->rx_buf_missing;
  782. /* Copy the rest of the bytes that are left in the
  783. * buffer into the waiting sk_buf. */
  784. /* Make room for temp_bytes after tail. */
  785. skb_put_data(odev->skb_rx_buf,
  786. ip_pkt + buffer_offset,
  787. temp_bytes);
  788. odev->rx_buf_missing -= temp_bytes;
  789. count -= temp_bytes;
  790. buffer_offset += temp_bytes;
  791. odev->rx_buf_size += temp_bytes;
  792. if (!odev->rx_buf_missing) {
  793. /* Packet is complete. Inject into stack. */
  794. /* We have IP packet here */
  795. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  796. skb_reset_mac_header(odev->skb_rx_buf);
  797. /* Ship it off to the kernel */
  798. netif_rx(odev->skb_rx_buf);
  799. /* No longer our buffer. */
  800. odev->skb_rx_buf = NULL;
  801. /* update out statistics */
  802. odev->net->stats.rx_packets++;
  803. odev->net->stats.rx_bytes += odev->rx_buf_size;
  804. odev->rx_buf_size = 0;
  805. odev->rx_buf_missing = sizeof(struct iphdr);
  806. odev->rx_parse_state = WAIT_IP;
  807. }
  808. break;
  809. case WAIT_SYNC:
  810. hso_dbg(0x1, " W_S\n");
  811. count = 0;
  812. break;
  813. default:
  814. hso_dbg(0x1, "\n");
  815. count--;
  816. break;
  817. }
  818. }
  819. /* Recovery mechanism for WAIT_SYNC state. */
  820. if (is_eop) {
  821. if (odev->rx_parse_state == WAIT_SYNC) {
  822. odev->rx_parse_state = WAIT_IP;
  823. odev->rx_buf_size = 0;
  824. odev->rx_buf_missing = sizeof(struct iphdr);
  825. }
  826. }
  827. }
  828. static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
  829. {
  830. static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  831. u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
  832. if (((rest == 5) || (rest == 6)) &&
  833. !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
  834. crc_check, 4)) {
  835. urb->actual_length -= 4;
  836. }
  837. }
  838. /* Moving data from usb to kernel (in interrupt state) */
  839. static void read_bulk_callback(struct urb *urb)
  840. {
  841. struct hso_net *odev = urb->context;
  842. struct net_device *net;
  843. int result;
  844. unsigned long flags;
  845. int status = urb->status;
  846. /* is al ok? (Filip: Who's Al ?) */
  847. if (status) {
  848. handle_usb_error(status, __func__, odev->parent);
  849. return;
  850. }
  851. /* Sanity check */
  852. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  853. hso_dbg(0x1, "BULK IN callback but driver is not active!\n");
  854. return;
  855. }
  856. usb_mark_last_busy(urb->dev);
  857. net = odev->net;
  858. if (!netif_device_present(net)) {
  859. /* Somebody killed our network interface... */
  860. return;
  861. }
  862. if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
  863. fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
  864. /* do we even have a packet? */
  865. if (urb->actual_length) {
  866. /* Handle the IP stream, add header and push it onto network
  867. * stack if the packet is complete. */
  868. spin_lock_irqsave(&odev->net_lock, flags);
  869. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  870. (urb->transfer_buffer_length >
  871. urb->actual_length) ? 1 : 0);
  872. spin_unlock_irqrestore(&odev->net_lock, flags);
  873. }
  874. /* We are done with this URB, resubmit it. Prep the USB to wait for
  875. * another frame. Reuse same as received. */
  876. usb_fill_bulk_urb(urb,
  877. odev->parent->usb,
  878. usb_rcvbulkpipe(odev->parent->usb,
  879. odev->in_endp->
  880. bEndpointAddress & 0x7F),
  881. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  882. read_bulk_callback, odev);
  883. /* Give this to the USB subsystem so it can tell us when more data
  884. * arrives. */
  885. result = usb_submit_urb(urb, GFP_ATOMIC);
  886. if (result)
  887. dev_warn(&odev->parent->interface->dev,
  888. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  889. result);
  890. }
  891. /* Serial driver functions */
  892. static void hso_init_termios(struct ktermios *termios)
  893. {
  894. /*
  895. * The default requirements for this device are:
  896. */
  897. termios->c_iflag &=
  898. ~(IGNBRK /* disable ignore break */
  899. | BRKINT /* disable break causes interrupt */
  900. | PARMRK /* disable mark parity errors */
  901. | ISTRIP /* disable clear high bit of input characters */
  902. | INLCR /* disable translate NL to CR */
  903. | IGNCR /* disable ignore CR */
  904. | ICRNL /* disable translate CR to NL */
  905. | IXON); /* disable enable XON/XOFF flow control */
  906. /* disable postprocess output characters */
  907. termios->c_oflag &= ~OPOST;
  908. termios->c_lflag &=
  909. ~(ECHO /* disable echo input characters */
  910. | ECHONL /* disable echo new line */
  911. | ICANON /* disable erase, kill, werase, and rprnt
  912. special characters */
  913. | ISIG /* disable interrupt, quit, and suspend special
  914. characters */
  915. | IEXTEN); /* disable non-POSIX special characters */
  916. termios->c_cflag &=
  917. ~(CSIZE /* no size */
  918. | PARENB /* disable parity bit */
  919. | CBAUD /* clear current baud rate */
  920. | CBAUDEX); /* clear current buad rate */
  921. termios->c_cflag |= CS8; /* character size 8 bits */
  922. /* baud rate 115200 */
  923. tty_termios_encode_baud_rate(termios, 115200, 115200);
  924. }
  925. static void _hso_serial_set_termios(struct tty_struct *tty)
  926. {
  927. struct hso_serial *serial = tty->driver_data;
  928. if (!serial) {
  929. pr_err("%s: no tty structures", __func__);
  930. return;
  931. }
  932. hso_dbg(0x8, "port %d\n", serial->minor);
  933. /*
  934. * Fix up unsupported bits
  935. */
  936. tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  937. tty->termios.c_cflag &=
  938. ~(CSIZE /* no size */
  939. | PARENB /* disable parity bit */
  940. | CBAUD /* clear current baud rate */
  941. | CBAUDEX); /* clear current buad rate */
  942. tty->termios.c_cflag |= CS8; /* character size 8 bits */
  943. /* baud rate 115200 */
  944. tty_encode_baud_rate(tty, 115200, 115200);
  945. }
  946. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  947. {
  948. int result;
  949. /* We are done with this URB, resubmit it. Prep the USB to wait for
  950. * another frame */
  951. usb_fill_bulk_urb(urb, serial->parent->usb,
  952. usb_rcvbulkpipe(serial->parent->usb,
  953. serial->in_endp->
  954. bEndpointAddress & 0x7F),
  955. urb->transfer_buffer, serial->rx_data_length,
  956. hso_std_serial_read_bulk_callback, serial);
  957. /* Give this to the USB subsystem so it can tell us when more data
  958. * arrives. */
  959. result = usb_submit_urb(urb, GFP_ATOMIC);
  960. if (result) {
  961. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  962. __func__, result);
  963. }
  964. }
  965. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  966. {
  967. int count;
  968. struct urb *curr_urb;
  969. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  970. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  971. count = put_rxbuf_data(curr_urb, serial);
  972. if (count == -1)
  973. return;
  974. if (count == 0) {
  975. serial->curr_rx_urb_idx++;
  976. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  977. serial->curr_rx_urb_idx = 0;
  978. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  979. }
  980. }
  981. }
  982. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  983. {
  984. int count = 0;
  985. struct urb *urb;
  986. urb = serial->rx_urb[0];
  987. if (serial->port.count > 0) {
  988. count = put_rxbuf_data(urb, serial);
  989. if (count == -1)
  990. return;
  991. }
  992. /* Re issue a read as long as we receive data. */
  993. if (count == 0 && ((urb->actual_length != 0) ||
  994. (serial->rx_state == RX_PENDING))) {
  995. serial->rx_state = RX_SENT;
  996. hso_mux_serial_read(serial);
  997. } else
  998. serial->rx_state = RX_IDLE;
  999. }
  1000. /* read callback for Diag and CS port */
  1001. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1002. {
  1003. struct hso_serial *serial = urb->context;
  1004. int status = urb->status;
  1005. unsigned long flags;
  1006. hso_dbg(0x8, "--- Got serial_read_bulk callback %02x ---\n", status);
  1007. /* sanity check */
  1008. if (!serial) {
  1009. hso_dbg(0x1, "serial == NULL\n");
  1010. return;
  1011. }
  1012. if (status) {
  1013. handle_usb_error(status, __func__, serial->parent);
  1014. return;
  1015. }
  1016. hso_dbg(0x1, "Actual length = %d\n", urb->actual_length);
  1017. DUMP1(urb->transfer_buffer, urb->actual_length);
  1018. /* Anyone listening? */
  1019. if (serial->port.count == 0)
  1020. return;
  1021. if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
  1022. fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
  1023. /* Valid data, handle RX data */
  1024. spin_lock_irqsave(&serial->serial_lock, flags);
  1025. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1026. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1027. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1028. }
  1029. /*
  1030. * This needs to be a tasklet otherwise we will
  1031. * end up recursively calling this function.
  1032. */
  1033. static void hso_unthrottle_tasklet(struct tasklet_struct *t)
  1034. {
  1035. struct hso_serial *serial = from_tasklet(serial, t,
  1036. unthrottle_tasklet);
  1037. unsigned long flags;
  1038. spin_lock_irqsave(&serial->serial_lock, flags);
  1039. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1040. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1041. else
  1042. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1043. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1044. }
  1045. static void hso_unthrottle(struct tty_struct *tty)
  1046. {
  1047. struct hso_serial *serial = tty->driver_data;
  1048. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1049. }
  1050. /* open the requested serial port */
  1051. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1052. {
  1053. struct hso_serial *serial = get_serial_by_index(tty->index);
  1054. int result;
  1055. /* sanity check */
  1056. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1057. WARN_ON(1);
  1058. tty->driver_data = NULL;
  1059. hso_dbg(0x1, "Failed to open port\n");
  1060. return -ENODEV;
  1061. }
  1062. mutex_lock(&serial->parent->mutex);
  1063. result = usb_autopm_get_interface(serial->parent->interface);
  1064. if (result < 0)
  1065. goto err_out;
  1066. hso_dbg(0x1, "Opening %d\n", serial->minor);
  1067. /* setup */
  1068. tty->driver_data = serial;
  1069. tty_port_tty_set(&serial->port, tty);
  1070. /* check for port already opened, if not set the termios */
  1071. serial->port.count++;
  1072. if (serial->port.count == 1) {
  1073. serial->rx_state = RX_IDLE;
  1074. /* Force default termio settings */
  1075. _hso_serial_set_termios(tty);
  1076. tasklet_setup(&serial->unthrottle_tasklet,
  1077. hso_unthrottle_tasklet);
  1078. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1079. if (result) {
  1080. hso_stop_serial_device(serial->parent);
  1081. serial->port.count--;
  1082. } else {
  1083. kref_get(&serial->parent->ref);
  1084. }
  1085. } else {
  1086. hso_dbg(0x1, "Port was already open\n");
  1087. }
  1088. usb_autopm_put_interface(serial->parent->interface);
  1089. /* done */
  1090. if (result)
  1091. hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
  1092. err_out:
  1093. mutex_unlock(&serial->parent->mutex);
  1094. return result;
  1095. }
  1096. /* close the requested serial port */
  1097. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1098. {
  1099. struct hso_serial *serial = tty->driver_data;
  1100. u8 usb_gone;
  1101. hso_dbg(0x1, "Closing serial port\n");
  1102. /* Open failed, no close cleanup required */
  1103. if (serial == NULL)
  1104. return;
  1105. mutex_lock(&serial->parent->mutex);
  1106. usb_gone = serial->parent->usb_gone;
  1107. if (!usb_gone)
  1108. usb_autopm_get_interface(serial->parent->interface);
  1109. /* reset the rts and dtr */
  1110. /* do the actual close */
  1111. serial->port.count--;
  1112. if (serial->port.count <= 0) {
  1113. serial->port.count = 0;
  1114. tty_port_tty_set(&serial->port, NULL);
  1115. if (!usb_gone)
  1116. hso_stop_serial_device(serial->parent);
  1117. tasklet_kill(&serial->unthrottle_tasklet);
  1118. }
  1119. if (!usb_gone)
  1120. usb_autopm_put_interface(serial->parent->interface);
  1121. mutex_unlock(&serial->parent->mutex);
  1122. }
  1123. /* close the requested serial port */
  1124. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1125. int count)
  1126. {
  1127. struct hso_serial *serial = tty->driver_data;
  1128. int space, tx_bytes;
  1129. unsigned long flags;
  1130. /* sanity check */
  1131. if (serial == NULL) {
  1132. pr_err("%s: serial is NULL\n", __func__);
  1133. return -ENODEV;
  1134. }
  1135. spin_lock_irqsave(&serial->serial_lock, flags);
  1136. space = serial->tx_data_length - serial->tx_buffer_count;
  1137. tx_bytes = (count < space) ? count : space;
  1138. if (!tx_bytes)
  1139. goto out;
  1140. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1141. serial->tx_buffer_count += tx_bytes;
  1142. out:
  1143. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1144. hso_kick_transmit(serial);
  1145. /* done */
  1146. return tx_bytes;
  1147. }
  1148. /* how much room is there for writing */
  1149. static unsigned int hso_serial_write_room(struct tty_struct *tty)
  1150. {
  1151. struct hso_serial *serial = tty->driver_data;
  1152. unsigned int room;
  1153. unsigned long flags;
  1154. spin_lock_irqsave(&serial->serial_lock, flags);
  1155. room = serial->tx_data_length - serial->tx_buffer_count;
  1156. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1157. /* return free room */
  1158. return room;
  1159. }
  1160. static void hso_serial_cleanup(struct tty_struct *tty)
  1161. {
  1162. struct hso_serial *serial = tty->driver_data;
  1163. if (!serial)
  1164. return;
  1165. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1166. }
  1167. /* setup the term */
  1168. static void hso_serial_set_termios(struct tty_struct *tty,
  1169. const struct ktermios *old)
  1170. {
  1171. struct hso_serial *serial = tty->driver_data;
  1172. unsigned long flags;
  1173. if (old)
  1174. hso_dbg(0x16, "Termios called with: cflags new[%u] - old[%u]\n",
  1175. (unsigned int)tty->termios.c_cflag,
  1176. (unsigned int)old->c_cflag);
  1177. /* the actual setup */
  1178. spin_lock_irqsave(&serial->serial_lock, flags);
  1179. if (serial->port.count)
  1180. _hso_serial_set_termios(tty);
  1181. else
  1182. tty->termios = *old;
  1183. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1184. /* done */
  1185. }
  1186. /* how many characters in the buffer */
  1187. static unsigned int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1188. {
  1189. struct hso_serial *serial = tty->driver_data;
  1190. unsigned long flags;
  1191. unsigned int chars;
  1192. /* sanity check */
  1193. if (serial == NULL)
  1194. return 0;
  1195. spin_lock_irqsave(&serial->serial_lock, flags);
  1196. chars = serial->tx_buffer_count;
  1197. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1198. return chars;
  1199. }
  1200. static int tiocmget_submit_urb(struct hso_serial *serial,
  1201. struct hso_tiocmget *tiocmget,
  1202. struct usb_device *usb)
  1203. {
  1204. int result;
  1205. if (serial->parent->usb_gone)
  1206. return -ENODEV;
  1207. usb_fill_int_urb(tiocmget->urb, usb,
  1208. usb_rcvintpipe(usb,
  1209. tiocmget->endp->
  1210. bEndpointAddress & 0x7F),
  1211. tiocmget->serial_state_notification,
  1212. sizeof(struct hso_serial_state_notification),
  1213. tiocmget_intr_callback, serial,
  1214. tiocmget->endp->bInterval);
  1215. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1216. if (result) {
  1217. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1218. result);
  1219. }
  1220. return result;
  1221. }
  1222. static void tiocmget_intr_callback(struct urb *urb)
  1223. {
  1224. struct hso_serial *serial = urb->context;
  1225. struct hso_tiocmget *tiocmget;
  1226. int status = urb->status;
  1227. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1228. struct uart_icount *icount;
  1229. struct hso_serial_state_notification *serial_state_notification;
  1230. struct usb_device *usb;
  1231. struct usb_interface *interface;
  1232. int if_num;
  1233. /* Sanity checks */
  1234. if (!serial)
  1235. return;
  1236. if (status) {
  1237. handle_usb_error(status, __func__, serial->parent);
  1238. return;
  1239. }
  1240. /* tiocmget is only supported on HSO_PORT_MODEM */
  1241. tiocmget = serial->tiocmget;
  1242. if (!tiocmget)
  1243. return;
  1244. BUG_ON((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM);
  1245. usb = serial->parent->usb;
  1246. interface = serial->parent->interface;
  1247. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1248. /* wIndex should be the USB interface number of the port to which the
  1249. * notification applies, which should always be the Modem port.
  1250. */
  1251. serial_state_notification = tiocmget->serial_state_notification;
  1252. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1253. serial_state_notification->bNotification != B_NOTIFICATION ||
  1254. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1255. le16_to_cpu(serial_state_notification->wIndex) != if_num ||
  1256. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1257. dev_warn(&usb->dev,
  1258. "hso received invalid serial state notification\n");
  1259. DUMP(serial_state_notification,
  1260. sizeof(struct hso_serial_state_notification));
  1261. } else {
  1262. unsigned long flags;
  1263. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1264. UART_state_bitmap);
  1265. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1266. icount = &tiocmget->icount;
  1267. spin_lock_irqsave(&serial->serial_lock, flags);
  1268. if ((UART_state_bitmap & B_OVERRUN) !=
  1269. (prev_UART_state_bitmap & B_OVERRUN))
  1270. icount->parity++;
  1271. if ((UART_state_bitmap & B_PARITY) !=
  1272. (prev_UART_state_bitmap & B_PARITY))
  1273. icount->parity++;
  1274. if ((UART_state_bitmap & B_FRAMING) !=
  1275. (prev_UART_state_bitmap & B_FRAMING))
  1276. icount->frame++;
  1277. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1278. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1279. icount->rng++;
  1280. if ((UART_state_bitmap & B_BREAK) !=
  1281. (prev_UART_state_bitmap & B_BREAK))
  1282. icount->brk++;
  1283. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1284. (prev_UART_state_bitmap & B_TX_CARRIER))
  1285. icount->dsr++;
  1286. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1287. (prev_UART_state_bitmap & B_RX_CARRIER))
  1288. icount->dcd++;
  1289. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1290. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1291. tiocmget->intr_completed = 1;
  1292. wake_up_interruptible(&tiocmget->waitq);
  1293. }
  1294. memset(serial_state_notification, 0,
  1295. sizeof(struct hso_serial_state_notification));
  1296. tiocmget_submit_urb(serial,
  1297. tiocmget,
  1298. serial->parent->usb);
  1299. }
  1300. /*
  1301. * next few functions largely stolen from drivers/serial/serial_core.c
  1302. */
  1303. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1304. * - mask passed in arg for lines of interest
  1305. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1306. * Caller should use TIOCGICOUNT to see which one it was
  1307. */
  1308. static int
  1309. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1310. {
  1311. DECLARE_WAITQUEUE(wait, current);
  1312. struct uart_icount cprev, cnow;
  1313. struct hso_tiocmget *tiocmget;
  1314. int ret;
  1315. tiocmget = serial->tiocmget;
  1316. if (!tiocmget)
  1317. return -ENOENT;
  1318. /*
  1319. * note the counters on entry
  1320. */
  1321. spin_lock_irq(&serial->serial_lock);
  1322. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1323. spin_unlock_irq(&serial->serial_lock);
  1324. add_wait_queue(&tiocmget->waitq, &wait);
  1325. for (;;) {
  1326. spin_lock_irq(&serial->serial_lock);
  1327. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1328. spin_unlock_irq(&serial->serial_lock);
  1329. set_current_state(TASK_INTERRUPTIBLE);
  1330. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1331. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1332. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1333. ret = 0;
  1334. break;
  1335. }
  1336. schedule();
  1337. /* see if a signal did it */
  1338. if (signal_pending(current)) {
  1339. ret = -ERESTARTSYS;
  1340. break;
  1341. }
  1342. cprev = cnow;
  1343. }
  1344. __set_current_state(TASK_RUNNING);
  1345. remove_wait_queue(&tiocmget->waitq, &wait);
  1346. return ret;
  1347. }
  1348. /*
  1349. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1350. * Return: write counters to the user passed counter struct
  1351. * NB: both 1->0 and 0->1 transitions are counted except for
  1352. * RI where only 0->1 is counted.
  1353. */
  1354. static int hso_get_count(struct tty_struct *tty,
  1355. struct serial_icounter_struct *icount)
  1356. {
  1357. struct uart_icount cnow;
  1358. struct hso_serial *serial = tty->driver_data;
  1359. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1360. memset(icount, 0, sizeof(struct serial_icounter_struct));
  1361. if (!tiocmget)
  1362. return -ENOENT;
  1363. spin_lock_irq(&serial->serial_lock);
  1364. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1365. spin_unlock_irq(&serial->serial_lock);
  1366. icount->cts = cnow.cts;
  1367. icount->dsr = cnow.dsr;
  1368. icount->rng = cnow.rng;
  1369. icount->dcd = cnow.dcd;
  1370. icount->rx = cnow.rx;
  1371. icount->tx = cnow.tx;
  1372. icount->frame = cnow.frame;
  1373. icount->overrun = cnow.overrun;
  1374. icount->parity = cnow.parity;
  1375. icount->brk = cnow.brk;
  1376. icount->buf_overrun = cnow.buf_overrun;
  1377. return 0;
  1378. }
  1379. static int hso_serial_tiocmget(struct tty_struct *tty)
  1380. {
  1381. int retval;
  1382. struct hso_serial *serial = tty->driver_data;
  1383. struct hso_tiocmget *tiocmget;
  1384. u16 UART_state_bitmap;
  1385. /* sanity check */
  1386. if (!serial) {
  1387. hso_dbg(0x1, "no tty structures\n");
  1388. return -EINVAL;
  1389. }
  1390. spin_lock_irq(&serial->serial_lock);
  1391. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1392. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1393. tiocmget = serial->tiocmget;
  1394. if (tiocmget) {
  1395. UART_state_bitmap = le16_to_cpu(
  1396. tiocmget->prev_UART_state_bitmap);
  1397. if (UART_state_bitmap & B_RING_SIGNAL)
  1398. retval |= TIOCM_RNG;
  1399. if (UART_state_bitmap & B_RX_CARRIER)
  1400. retval |= TIOCM_CD;
  1401. if (UART_state_bitmap & B_TX_CARRIER)
  1402. retval |= TIOCM_DSR;
  1403. }
  1404. spin_unlock_irq(&serial->serial_lock);
  1405. return retval;
  1406. }
  1407. static int hso_serial_tiocmset(struct tty_struct *tty,
  1408. unsigned int set, unsigned int clear)
  1409. {
  1410. int val = 0;
  1411. unsigned long flags;
  1412. int if_num;
  1413. struct hso_serial *serial = tty->driver_data;
  1414. struct usb_interface *interface;
  1415. /* sanity check */
  1416. if (!serial) {
  1417. hso_dbg(0x1, "no tty structures\n");
  1418. return -EINVAL;
  1419. }
  1420. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1421. return -EINVAL;
  1422. interface = serial->parent->interface;
  1423. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1424. spin_lock_irqsave(&serial->serial_lock, flags);
  1425. if (set & TIOCM_RTS)
  1426. serial->rts_state = 1;
  1427. if (set & TIOCM_DTR)
  1428. serial->dtr_state = 1;
  1429. if (clear & TIOCM_RTS)
  1430. serial->rts_state = 0;
  1431. if (clear & TIOCM_DTR)
  1432. serial->dtr_state = 0;
  1433. if (serial->dtr_state)
  1434. val |= 0x01;
  1435. if (serial->rts_state)
  1436. val |= 0x02;
  1437. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1438. return usb_control_msg(serial->parent->usb,
  1439. usb_sndctrlpipe(serial->parent->usb, 0), 0x22,
  1440. 0x21, val, if_num, NULL, 0,
  1441. USB_CTRL_SET_TIMEOUT);
  1442. }
  1443. static int hso_serial_ioctl(struct tty_struct *tty,
  1444. unsigned int cmd, unsigned long arg)
  1445. {
  1446. struct hso_serial *serial = tty->driver_data;
  1447. int ret = 0;
  1448. hso_dbg(0x8, "IOCTL cmd: %d, arg: %ld\n", cmd, arg);
  1449. if (!serial)
  1450. return -ENODEV;
  1451. switch (cmd) {
  1452. case TIOCMIWAIT:
  1453. ret = hso_wait_modem_status(serial, arg);
  1454. break;
  1455. default:
  1456. ret = -ENOIOCTLCMD;
  1457. break;
  1458. }
  1459. return ret;
  1460. }
  1461. /* starts a transmit */
  1462. static void hso_kick_transmit(struct hso_serial *serial)
  1463. {
  1464. unsigned long flags;
  1465. int res;
  1466. spin_lock_irqsave(&serial->serial_lock, flags);
  1467. if (!serial->tx_buffer_count)
  1468. goto out;
  1469. if (serial->tx_urb_used)
  1470. goto out;
  1471. /* Wakeup USB interface if necessary */
  1472. if (hso_get_activity(serial->parent) == -EAGAIN)
  1473. goto out;
  1474. /* Switch pointers around to avoid memcpy */
  1475. swap(serial->tx_buffer, serial->tx_data);
  1476. serial->tx_data_count = serial->tx_buffer_count;
  1477. serial->tx_buffer_count = 0;
  1478. /* If serial->tx_data is set, it means we switched buffers */
  1479. if (serial->tx_data && serial->write_data) {
  1480. res = serial->write_data(serial);
  1481. if (res >= 0)
  1482. serial->tx_urb_used = 1;
  1483. }
  1484. out:
  1485. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1486. }
  1487. /* make a request (for reading and writing data to muxed serial port) */
  1488. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1489. struct urb *ctrl_urb,
  1490. struct usb_ctrlrequest *ctrl_req,
  1491. u8 *ctrl_urb_data, u32 size)
  1492. {
  1493. int result;
  1494. int pipe;
  1495. /* Sanity check */
  1496. if (!serial || !ctrl_urb || !ctrl_req) {
  1497. pr_err("%s: Wrong arguments\n", __func__);
  1498. return -EINVAL;
  1499. }
  1500. /* initialize */
  1501. ctrl_req->wValue = 0;
  1502. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1503. ctrl_req->wLength = cpu_to_le16(size);
  1504. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1505. /* Reading command */
  1506. ctrl_req->bRequestType = USB_DIR_IN |
  1507. USB_TYPE_OPTION_VENDOR |
  1508. USB_RECIP_INTERFACE;
  1509. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1510. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1511. } else {
  1512. /* Writing command */
  1513. ctrl_req->bRequestType = USB_DIR_OUT |
  1514. USB_TYPE_OPTION_VENDOR |
  1515. USB_RECIP_INTERFACE;
  1516. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1517. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1518. }
  1519. /* syslog */
  1520. hso_dbg(0x2, "%s command (%02x) len: %d, port: %d\n",
  1521. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1522. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1523. /* Load ctrl urb */
  1524. ctrl_urb->transfer_flags = 0;
  1525. usb_fill_control_urb(ctrl_urb,
  1526. serial->parent->usb,
  1527. pipe,
  1528. (u8 *) ctrl_req,
  1529. ctrl_urb_data, size, ctrl_callback, serial);
  1530. /* Send it on merry way */
  1531. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1532. if (result) {
  1533. dev_err(&ctrl_urb->dev->dev,
  1534. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1535. result, type);
  1536. return result;
  1537. }
  1538. /* done */
  1539. return size;
  1540. }
  1541. /* called by intr_callback when read occurs */
  1542. static int hso_mux_serial_read(struct hso_serial *serial)
  1543. {
  1544. if (!serial)
  1545. return -EINVAL;
  1546. /* clean data */
  1547. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1548. /* make the request */
  1549. if (serial->num_rx_urbs != 1) {
  1550. dev_err(&serial->parent->interface->dev,
  1551. "ERROR: mux'd reads with multiple buffers "
  1552. "not possible\n");
  1553. return 0;
  1554. }
  1555. return mux_device_request(serial,
  1556. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1557. serial->parent->port_spec & HSO_PORT_MASK,
  1558. serial->rx_urb[0],
  1559. &serial->ctrl_req_rx,
  1560. serial->rx_data[0], serial->rx_data_length);
  1561. }
  1562. /* used for muxed serial port callback (muxed serial read) */
  1563. static void intr_callback(struct urb *urb)
  1564. {
  1565. struct hso_shared_int *shared_int = urb->context;
  1566. struct hso_serial *serial;
  1567. unsigned char *port_req;
  1568. int status = urb->status;
  1569. unsigned long flags;
  1570. int i;
  1571. usb_mark_last_busy(urb->dev);
  1572. /* sanity check */
  1573. if (!shared_int)
  1574. return;
  1575. /* status check */
  1576. if (status) {
  1577. handle_usb_error(status, __func__, NULL);
  1578. return;
  1579. }
  1580. hso_dbg(0x8, "--- Got intr callback 0x%02X ---\n", status);
  1581. /* what request? */
  1582. port_req = urb->transfer_buffer;
  1583. hso_dbg(0x8, "port_req = 0x%.2X\n", *port_req);
  1584. /* loop over all muxed ports to find the one sending this */
  1585. for (i = 0; i < 8; i++) {
  1586. /* max 8 channels on MUX */
  1587. if (*port_req & (1 << i)) {
  1588. serial = get_serial_by_shared_int_and_type(shared_int,
  1589. (1 << i));
  1590. if (serial != NULL) {
  1591. hso_dbg(0x1, "Pending read interrupt on port %d\n",
  1592. i);
  1593. spin_lock_irqsave(&serial->serial_lock, flags);
  1594. if (serial->rx_state == RX_IDLE &&
  1595. serial->port.count > 0) {
  1596. /* Setup and send a ctrl req read on
  1597. * port i */
  1598. if (!serial->rx_urb_filled[0]) {
  1599. serial->rx_state = RX_SENT;
  1600. hso_mux_serial_read(serial);
  1601. } else
  1602. serial->rx_state = RX_PENDING;
  1603. } else {
  1604. hso_dbg(0x1, "Already a read pending on port %d or port not open\n",
  1605. i);
  1606. }
  1607. spin_unlock_irqrestore(&serial->serial_lock,
  1608. flags);
  1609. }
  1610. }
  1611. }
  1612. /* Resubmit interrupt urb */
  1613. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1614. }
  1615. /* called for writing to muxed serial port */
  1616. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1617. {
  1618. if (NULL == serial)
  1619. return -EINVAL;
  1620. return mux_device_request(serial,
  1621. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1622. serial->parent->port_spec & HSO_PORT_MASK,
  1623. serial->tx_urb,
  1624. &serial->ctrl_req_tx,
  1625. serial->tx_data, serial->tx_data_count);
  1626. }
  1627. /* write callback for Diag and CS port */
  1628. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1629. {
  1630. struct hso_serial *serial = urb->context;
  1631. int status = urb->status;
  1632. unsigned long flags;
  1633. /* sanity check */
  1634. if (!serial) {
  1635. hso_dbg(0x1, "serial == NULL\n");
  1636. return;
  1637. }
  1638. spin_lock_irqsave(&serial->serial_lock, flags);
  1639. serial->tx_urb_used = 0;
  1640. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1641. if (status) {
  1642. handle_usb_error(status, __func__, serial->parent);
  1643. return;
  1644. }
  1645. hso_put_activity(serial->parent);
  1646. tty_port_tty_wakeup(&serial->port);
  1647. hso_kick_transmit(serial);
  1648. hso_dbg(0x1, "\n");
  1649. }
  1650. /* called for writing diag or CS serial port */
  1651. static int hso_std_serial_write_data(struct hso_serial *serial)
  1652. {
  1653. int count = serial->tx_data_count;
  1654. int result;
  1655. usb_fill_bulk_urb(serial->tx_urb,
  1656. serial->parent->usb,
  1657. usb_sndbulkpipe(serial->parent->usb,
  1658. serial->out_endp->
  1659. bEndpointAddress & 0x7F),
  1660. serial->tx_data, serial->tx_data_count,
  1661. hso_std_serial_write_bulk_callback, serial);
  1662. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1663. if (result) {
  1664. dev_warn(&serial->parent->usb->dev,
  1665. "Failed to submit urb - res %d\n", result);
  1666. return result;
  1667. }
  1668. return count;
  1669. }
  1670. /* callback after read or write on muxed serial port */
  1671. static void ctrl_callback(struct urb *urb)
  1672. {
  1673. struct hso_serial *serial = urb->context;
  1674. struct usb_ctrlrequest *req;
  1675. int status = urb->status;
  1676. unsigned long flags;
  1677. /* sanity check */
  1678. if (!serial)
  1679. return;
  1680. spin_lock_irqsave(&serial->serial_lock, flags);
  1681. serial->tx_urb_used = 0;
  1682. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1683. if (status) {
  1684. handle_usb_error(status, __func__, serial->parent);
  1685. return;
  1686. }
  1687. /* what request? */
  1688. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1689. hso_dbg(0x8, "--- Got muxed ctrl callback 0x%02X ---\n", status);
  1690. hso_dbg(0x8, "Actual length of urb = %d\n", urb->actual_length);
  1691. DUMP1(urb->transfer_buffer, urb->actual_length);
  1692. if (req->bRequestType ==
  1693. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1694. /* response to a read command */
  1695. serial->rx_urb_filled[0] = 1;
  1696. spin_lock_irqsave(&serial->serial_lock, flags);
  1697. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1698. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1699. } else {
  1700. hso_put_activity(serial->parent);
  1701. tty_port_tty_wakeup(&serial->port);
  1702. /* response to a write command */
  1703. hso_kick_transmit(serial);
  1704. }
  1705. }
  1706. /* handle RX data for serial port */
  1707. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1708. {
  1709. struct tty_struct *tty;
  1710. int count;
  1711. /* Sanity check */
  1712. if (urb == NULL || serial == NULL) {
  1713. hso_dbg(0x1, "serial = NULL\n");
  1714. return -2;
  1715. }
  1716. tty = tty_port_tty_get(&serial->port);
  1717. if (tty && tty_throttled(tty)) {
  1718. tty_kref_put(tty);
  1719. return -1;
  1720. }
  1721. /* Push data to tty */
  1722. hso_dbg(0x1, "data to push to tty\n");
  1723. count = tty_buffer_request_room(&serial->port, urb->actual_length);
  1724. if (count >= urb->actual_length) {
  1725. tty_insert_flip_string(&serial->port, urb->transfer_buffer,
  1726. urb->actual_length);
  1727. tty_flip_buffer_push(&serial->port);
  1728. } else {
  1729. dev_warn(&serial->parent->usb->dev,
  1730. "dropping data, %d bytes lost\n", urb->actual_length);
  1731. }
  1732. tty_kref_put(tty);
  1733. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1734. return 0;
  1735. }
  1736. /* Base driver functions */
  1737. static void hso_log_port(struct hso_device *hso_dev)
  1738. {
  1739. char *port_type;
  1740. char port_dev[20];
  1741. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1742. case HSO_PORT_CONTROL:
  1743. port_type = "Control";
  1744. break;
  1745. case HSO_PORT_APP:
  1746. port_type = "Application";
  1747. break;
  1748. case HSO_PORT_GPS:
  1749. port_type = "GPS";
  1750. break;
  1751. case HSO_PORT_GPS_CONTROL:
  1752. port_type = "GPS control";
  1753. break;
  1754. case HSO_PORT_APP2:
  1755. port_type = "Application2";
  1756. break;
  1757. case HSO_PORT_PCSC:
  1758. port_type = "PCSC";
  1759. break;
  1760. case HSO_PORT_DIAG:
  1761. port_type = "Diagnostic";
  1762. break;
  1763. case HSO_PORT_DIAG2:
  1764. port_type = "Diagnostic2";
  1765. break;
  1766. case HSO_PORT_MODEM:
  1767. port_type = "Modem";
  1768. break;
  1769. case HSO_PORT_NETWORK:
  1770. port_type = "Network";
  1771. break;
  1772. default:
  1773. port_type = "Unknown";
  1774. break;
  1775. }
  1776. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1777. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1778. } else
  1779. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1780. dev2ser(hso_dev)->minor);
  1781. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1782. port_type, port_dev);
  1783. }
  1784. static int hso_start_net_device(struct hso_device *hso_dev)
  1785. {
  1786. int i, result = 0;
  1787. struct hso_net *hso_net = dev2net(hso_dev);
  1788. if (!hso_net)
  1789. return -ENODEV;
  1790. /* send URBs for all read buffers */
  1791. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1792. /* Prep a receive URB */
  1793. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1794. hso_dev->usb,
  1795. usb_rcvbulkpipe(hso_dev->usb,
  1796. hso_net->in_endp->
  1797. bEndpointAddress & 0x7F),
  1798. hso_net->mux_bulk_rx_buf_pool[i],
  1799. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1800. hso_net);
  1801. /* Put it out there so the device can send us stuff */
  1802. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1803. GFP_NOIO);
  1804. if (result)
  1805. dev_warn(&hso_dev->usb->dev,
  1806. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1807. i, result);
  1808. }
  1809. return result;
  1810. }
  1811. static int hso_stop_net_device(struct hso_device *hso_dev)
  1812. {
  1813. int i;
  1814. struct hso_net *hso_net = dev2net(hso_dev);
  1815. if (!hso_net)
  1816. return -ENODEV;
  1817. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1818. if (hso_net->mux_bulk_rx_urb_pool[i])
  1819. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1820. }
  1821. if (hso_net->mux_bulk_tx_urb)
  1822. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1823. return 0;
  1824. }
  1825. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1826. {
  1827. int i, result = 0;
  1828. struct hso_serial *serial = dev2ser(hso_dev);
  1829. if (!serial)
  1830. return -ENODEV;
  1831. /* If it is not the MUX port fill in and submit a bulk urb (already
  1832. * allocated in hso_serial_start) */
  1833. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1834. for (i = 0; i < serial->num_rx_urbs; i++) {
  1835. usb_fill_bulk_urb(serial->rx_urb[i],
  1836. serial->parent->usb,
  1837. usb_rcvbulkpipe(serial->parent->usb,
  1838. serial->in_endp->
  1839. bEndpointAddress &
  1840. 0x7F),
  1841. serial->rx_data[i],
  1842. serial->rx_data_length,
  1843. hso_std_serial_read_bulk_callback,
  1844. serial);
  1845. result = usb_submit_urb(serial->rx_urb[i], flags);
  1846. if (result) {
  1847. dev_warn(&serial->parent->usb->dev,
  1848. "Failed to submit urb - res %d\n",
  1849. result);
  1850. break;
  1851. }
  1852. }
  1853. } else {
  1854. mutex_lock(&serial->shared_int->shared_int_lock);
  1855. if (!serial->shared_int->use_count) {
  1856. result =
  1857. hso_mux_submit_intr_urb(serial->shared_int,
  1858. hso_dev->usb, flags);
  1859. }
  1860. serial->shared_int->use_count++;
  1861. mutex_unlock(&serial->shared_int->shared_int_lock);
  1862. }
  1863. if (serial->tiocmget)
  1864. tiocmget_submit_urb(serial,
  1865. serial->tiocmget,
  1866. serial->parent->usb);
  1867. return result;
  1868. }
  1869. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1870. {
  1871. int i;
  1872. struct hso_serial *serial = dev2ser(hso_dev);
  1873. struct hso_tiocmget *tiocmget;
  1874. if (!serial)
  1875. return -ENODEV;
  1876. for (i = 0; i < serial->num_rx_urbs; i++) {
  1877. if (serial->rx_urb[i]) {
  1878. usb_kill_urb(serial->rx_urb[i]);
  1879. serial->rx_urb_filled[i] = 0;
  1880. }
  1881. }
  1882. serial->curr_rx_urb_idx = 0;
  1883. if (serial->tx_urb)
  1884. usb_kill_urb(serial->tx_urb);
  1885. if (serial->shared_int) {
  1886. mutex_lock(&serial->shared_int->shared_int_lock);
  1887. if (serial->shared_int->use_count &&
  1888. (--serial->shared_int->use_count == 0)) {
  1889. struct urb *urb;
  1890. urb = serial->shared_int->shared_intr_urb;
  1891. if (urb)
  1892. usb_kill_urb(urb);
  1893. }
  1894. mutex_unlock(&serial->shared_int->shared_int_lock);
  1895. }
  1896. tiocmget = serial->tiocmget;
  1897. if (tiocmget) {
  1898. wake_up_interruptible(&tiocmget->waitq);
  1899. usb_kill_urb(tiocmget->urb);
  1900. }
  1901. return 0;
  1902. }
  1903. static void hso_serial_tty_unregister(struct hso_serial *serial)
  1904. {
  1905. tty_unregister_device(tty_drv, serial->minor);
  1906. release_minor(serial);
  1907. }
  1908. static void hso_serial_common_free(struct hso_serial *serial)
  1909. {
  1910. int i;
  1911. for (i = 0; i < serial->num_rx_urbs; i++) {
  1912. /* unlink and free RX URB */
  1913. usb_free_urb(serial->rx_urb[i]);
  1914. /* free the RX buffer */
  1915. kfree(serial->rx_data[i]);
  1916. }
  1917. /* unlink and free TX URB */
  1918. usb_free_urb(serial->tx_urb);
  1919. kfree(serial->tx_buffer);
  1920. kfree(serial->tx_data);
  1921. tty_port_destroy(&serial->port);
  1922. }
  1923. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1924. int rx_size, int tx_size)
  1925. {
  1926. int i;
  1927. tty_port_init(&serial->port);
  1928. if (obtain_minor(serial))
  1929. goto exit2;
  1930. /* register our minor number */
  1931. serial->parent->dev = tty_port_register_device_attr(&serial->port,
  1932. tty_drv, serial->minor, &serial->parent->interface->dev,
  1933. serial->parent, hso_serial_dev_groups);
  1934. if (IS_ERR(serial->parent->dev)) {
  1935. release_minor(serial);
  1936. goto exit2;
  1937. }
  1938. serial->magic = HSO_SERIAL_MAGIC;
  1939. spin_lock_init(&serial->serial_lock);
  1940. serial->num_rx_urbs = num_urbs;
  1941. /* RX, allocate urb and initialize */
  1942. /* prepare our RX buffer */
  1943. serial->rx_data_length = rx_size;
  1944. for (i = 0; i < serial->num_rx_urbs; i++) {
  1945. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1946. if (!serial->rx_urb[i])
  1947. goto exit;
  1948. serial->rx_urb[i]->transfer_buffer = NULL;
  1949. serial->rx_urb[i]->transfer_buffer_length = 0;
  1950. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1951. GFP_KERNEL);
  1952. if (!serial->rx_data[i])
  1953. goto exit;
  1954. }
  1955. /* TX, allocate urb and initialize */
  1956. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1957. if (!serial->tx_urb)
  1958. goto exit;
  1959. serial->tx_urb->transfer_buffer = NULL;
  1960. serial->tx_urb->transfer_buffer_length = 0;
  1961. /* prepare our TX buffer */
  1962. serial->tx_data_count = 0;
  1963. serial->tx_buffer_count = 0;
  1964. serial->tx_data_length = tx_size;
  1965. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1966. if (!serial->tx_data)
  1967. goto exit;
  1968. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1969. if (!serial->tx_buffer)
  1970. goto exit;
  1971. return 0;
  1972. exit:
  1973. hso_serial_tty_unregister(serial);
  1974. exit2:
  1975. hso_serial_common_free(serial);
  1976. return -1;
  1977. }
  1978. /* Creates a general hso device */
  1979. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1980. int port_spec)
  1981. {
  1982. struct hso_device *hso_dev;
  1983. hso_dev = kzalloc(sizeof(*hso_dev), GFP_KERNEL);
  1984. if (!hso_dev)
  1985. return NULL;
  1986. hso_dev->port_spec = port_spec;
  1987. hso_dev->usb = interface_to_usbdev(intf);
  1988. hso_dev->interface = intf;
  1989. kref_init(&hso_dev->ref);
  1990. mutex_init(&hso_dev->mutex);
  1991. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1992. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1993. return hso_dev;
  1994. }
  1995. /* Removes a network device in the network device table */
  1996. static int remove_net_device(struct hso_device *hso_dev)
  1997. {
  1998. int i;
  1999. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2000. if (network_table[i] == hso_dev) {
  2001. network_table[i] = NULL;
  2002. break;
  2003. }
  2004. }
  2005. if (i == HSO_MAX_NET_DEVICES)
  2006. return -1;
  2007. return 0;
  2008. }
  2009. /* Frees our network device */
  2010. static void hso_free_net_device(struct hso_device *hso_dev)
  2011. {
  2012. int i;
  2013. struct hso_net *hso_net = dev2net(hso_dev);
  2014. if (!hso_net)
  2015. return;
  2016. remove_net_device(hso_net->parent);
  2017. if (hso_net->net)
  2018. unregister_netdev(hso_net->net);
  2019. /* start freeing */
  2020. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2021. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2022. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2023. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2024. }
  2025. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2026. kfree(hso_net->mux_bulk_tx_buf);
  2027. hso_net->mux_bulk_tx_buf = NULL;
  2028. if (hso_net->net)
  2029. free_netdev(hso_net->net);
  2030. kfree(hso_dev);
  2031. }
  2032. static const struct net_device_ops hso_netdev_ops = {
  2033. .ndo_open = hso_net_open,
  2034. .ndo_stop = hso_net_close,
  2035. .ndo_start_xmit = hso_net_start_xmit,
  2036. .ndo_tx_timeout = hso_net_tx_timeout,
  2037. };
  2038. /* initialize the network interface */
  2039. static void hso_net_init(struct net_device *net)
  2040. {
  2041. struct hso_net *hso_net = netdev_priv(net);
  2042. hso_dbg(0x1, "sizeof hso_net is %zu\n", sizeof(*hso_net));
  2043. /* fill in the other fields */
  2044. net->netdev_ops = &hso_netdev_ops;
  2045. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2046. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2047. net->type = ARPHRD_NONE;
  2048. net->mtu = DEFAULT_MTU - 14;
  2049. net->tx_queue_len = 10;
  2050. net->ethtool_ops = &ops;
  2051. /* and initialize the semaphore */
  2052. spin_lock_init(&hso_net->net_lock);
  2053. }
  2054. /* Adds a network device in the network device table */
  2055. static int add_net_device(struct hso_device *hso_dev)
  2056. {
  2057. int i;
  2058. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2059. if (network_table[i] == NULL) {
  2060. network_table[i] = hso_dev;
  2061. break;
  2062. }
  2063. }
  2064. if (i == HSO_MAX_NET_DEVICES)
  2065. return -1;
  2066. return 0;
  2067. }
  2068. static int hso_rfkill_set_block(void *data, bool blocked)
  2069. {
  2070. struct hso_device *hso_dev = data;
  2071. int enabled = !blocked;
  2072. int rv;
  2073. mutex_lock(&hso_dev->mutex);
  2074. if (hso_dev->usb_gone)
  2075. rv = 0;
  2076. else
  2077. rv = usb_control_msg(hso_dev->usb, usb_sndctrlpipe(hso_dev->usb, 0),
  2078. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2079. USB_CTRL_SET_TIMEOUT);
  2080. mutex_unlock(&hso_dev->mutex);
  2081. return rv;
  2082. }
  2083. static const struct rfkill_ops hso_rfkill_ops = {
  2084. .set_block = hso_rfkill_set_block,
  2085. };
  2086. /* Creates and sets up everything for rfkill */
  2087. static void hso_create_rfkill(struct hso_device *hso_dev,
  2088. struct usb_interface *interface)
  2089. {
  2090. struct hso_net *hso_net = dev2net(hso_dev);
  2091. struct device *dev = &hso_net->net->dev;
  2092. static u32 rfkill_counter;
  2093. snprintf(hso_net->name, sizeof(hso_net->name), "hso-%d",
  2094. rfkill_counter++);
  2095. hso_net->rfkill = rfkill_alloc(hso_net->name,
  2096. &interface_to_usbdev(interface)->dev,
  2097. RFKILL_TYPE_WWAN,
  2098. &hso_rfkill_ops, hso_dev);
  2099. if (!hso_net->rfkill)
  2100. return;
  2101. if (rfkill_register(hso_net->rfkill) < 0) {
  2102. rfkill_destroy(hso_net->rfkill);
  2103. hso_net->rfkill = NULL;
  2104. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2105. return;
  2106. }
  2107. }
  2108. static struct device_type hso_type = {
  2109. .name = "wwan",
  2110. };
  2111. /* Creates our network device */
  2112. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2113. int port_spec)
  2114. {
  2115. int result, i;
  2116. struct net_device *net;
  2117. struct hso_net *hso_net;
  2118. struct hso_device *hso_dev;
  2119. hso_dev = hso_create_device(interface, port_spec);
  2120. if (!hso_dev)
  2121. return NULL;
  2122. /* allocate our network device, then we can put in our private data */
  2123. /* call hso_net_init to do the basic initialization */
  2124. net = alloc_netdev(sizeof(struct hso_net), "hso%d", NET_NAME_UNKNOWN,
  2125. hso_net_init);
  2126. if (!net) {
  2127. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2128. goto err_hso_dev;
  2129. }
  2130. hso_net = netdev_priv(net);
  2131. hso_dev->port_data.dev_net = hso_net;
  2132. hso_net->net = net;
  2133. hso_net->parent = hso_dev;
  2134. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2135. USB_DIR_IN);
  2136. if (!hso_net->in_endp) {
  2137. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2138. goto err_net;
  2139. }
  2140. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2141. USB_DIR_OUT);
  2142. if (!hso_net->out_endp) {
  2143. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2144. goto err_net;
  2145. }
  2146. SET_NETDEV_DEV(net, &interface->dev);
  2147. SET_NETDEV_DEVTYPE(net, &hso_type);
  2148. /* start allocating */
  2149. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2150. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2151. if (!hso_net->mux_bulk_rx_urb_pool[i])
  2152. goto err_mux_bulk_rx;
  2153. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2154. GFP_KERNEL);
  2155. if (!hso_net->mux_bulk_rx_buf_pool[i])
  2156. goto err_mux_bulk_rx;
  2157. }
  2158. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2159. if (!hso_net->mux_bulk_tx_urb)
  2160. goto err_mux_bulk_rx;
  2161. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2162. if (!hso_net->mux_bulk_tx_buf)
  2163. goto err_free_tx_urb;
  2164. result = add_net_device(hso_dev);
  2165. if (result) {
  2166. dev_err(&interface->dev, "Failed to add net device\n");
  2167. goto err_free_tx_buf;
  2168. }
  2169. /* registering our net device */
  2170. result = register_netdev(net);
  2171. if (result) {
  2172. dev_err(&interface->dev, "Failed to register device\n");
  2173. goto err_rmv_ndev;
  2174. }
  2175. hso_log_port(hso_dev);
  2176. hso_create_rfkill(hso_dev, interface);
  2177. return hso_dev;
  2178. err_rmv_ndev:
  2179. remove_net_device(hso_dev);
  2180. err_free_tx_buf:
  2181. kfree(hso_net->mux_bulk_tx_buf);
  2182. err_free_tx_urb:
  2183. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2184. err_mux_bulk_rx:
  2185. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2186. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2187. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2188. }
  2189. err_net:
  2190. free_netdev(net);
  2191. err_hso_dev:
  2192. kfree(hso_dev);
  2193. return NULL;
  2194. }
  2195. static void hso_free_tiomget(struct hso_serial *serial)
  2196. {
  2197. struct hso_tiocmget *tiocmget;
  2198. if (!serial)
  2199. return;
  2200. tiocmget = serial->tiocmget;
  2201. if (tiocmget) {
  2202. usb_free_urb(tiocmget->urb);
  2203. tiocmget->urb = NULL;
  2204. serial->tiocmget = NULL;
  2205. kfree(tiocmget->serial_state_notification);
  2206. tiocmget->serial_state_notification = NULL;
  2207. kfree(tiocmget);
  2208. }
  2209. }
  2210. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2211. static void hso_free_serial_device(struct hso_device *hso_dev)
  2212. {
  2213. struct hso_serial *serial = dev2ser(hso_dev);
  2214. if (!serial)
  2215. return;
  2216. hso_serial_common_free(serial);
  2217. if (serial->shared_int) {
  2218. mutex_lock(&serial->shared_int->shared_int_lock);
  2219. if (--serial->shared_int->ref_count == 0)
  2220. hso_free_shared_int(serial->shared_int);
  2221. else
  2222. mutex_unlock(&serial->shared_int->shared_int_lock);
  2223. }
  2224. hso_free_tiomget(serial);
  2225. kfree(serial);
  2226. kfree(hso_dev);
  2227. }
  2228. /* Creates a bulk AT channel */
  2229. static struct hso_device *hso_create_bulk_serial_device(
  2230. struct usb_interface *interface, int port)
  2231. {
  2232. struct hso_device *hso_dev;
  2233. struct hso_serial *serial;
  2234. int num_urbs;
  2235. struct hso_tiocmget *tiocmget;
  2236. hso_dev = hso_create_device(interface, port);
  2237. if (!hso_dev)
  2238. return NULL;
  2239. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2240. if (!serial)
  2241. goto exit;
  2242. serial->parent = hso_dev;
  2243. hso_dev->port_data.dev_serial = serial;
  2244. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2245. num_urbs = 2;
  2246. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2247. GFP_KERNEL);
  2248. if (!serial->tiocmget)
  2249. goto exit;
  2250. serial->tiocmget->serial_state_notification
  2251. = kzalloc(sizeof(struct hso_serial_state_notification),
  2252. GFP_KERNEL);
  2253. if (!serial->tiocmget->serial_state_notification)
  2254. goto exit;
  2255. tiocmget = serial->tiocmget;
  2256. tiocmget->endp = hso_get_ep(interface,
  2257. USB_ENDPOINT_XFER_INT,
  2258. USB_DIR_IN);
  2259. if (!tiocmget->endp) {
  2260. dev_err(&interface->dev, "Failed to find INT IN ep\n");
  2261. goto exit;
  2262. }
  2263. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2264. if (!tiocmget->urb)
  2265. goto exit;
  2266. mutex_init(&tiocmget->mutex);
  2267. init_waitqueue_head(&tiocmget->waitq);
  2268. } else {
  2269. num_urbs = 1;
  2270. }
  2271. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2272. BULK_URB_TX_SIZE))
  2273. goto exit;
  2274. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2275. USB_DIR_IN);
  2276. if (!serial->in_endp) {
  2277. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2278. goto exit2;
  2279. }
  2280. if (!
  2281. (serial->out_endp =
  2282. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2283. dev_err(&interface->dev, "Failed to find BULK OUT ep\n");
  2284. goto exit2;
  2285. }
  2286. serial->write_data = hso_std_serial_write_data;
  2287. /* setup the proc dirs and files if needed */
  2288. hso_log_port(hso_dev);
  2289. /* done, return it */
  2290. return hso_dev;
  2291. exit2:
  2292. hso_serial_tty_unregister(serial);
  2293. hso_serial_common_free(serial);
  2294. exit:
  2295. hso_free_tiomget(serial);
  2296. kfree(serial);
  2297. kfree(hso_dev);
  2298. return NULL;
  2299. }
  2300. /* Creates a multiplexed AT channel */
  2301. static
  2302. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2303. int port,
  2304. struct hso_shared_int *mux)
  2305. {
  2306. struct hso_device *hso_dev;
  2307. struct hso_serial *serial;
  2308. int port_spec;
  2309. port_spec = HSO_INTF_MUX;
  2310. port_spec &= ~HSO_PORT_MASK;
  2311. port_spec |= hso_mux_to_port(port);
  2312. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2313. return NULL;
  2314. hso_dev = hso_create_device(interface, port_spec);
  2315. if (!hso_dev)
  2316. return NULL;
  2317. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2318. if (!serial)
  2319. goto err_free_dev;
  2320. hso_dev->port_data.dev_serial = serial;
  2321. serial->parent = hso_dev;
  2322. if (hso_serial_common_create
  2323. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2324. goto err_free_serial;
  2325. serial->tx_data_length--;
  2326. serial->write_data = hso_mux_serial_write_data;
  2327. serial->shared_int = mux;
  2328. mutex_lock(&serial->shared_int->shared_int_lock);
  2329. serial->shared_int->ref_count++;
  2330. mutex_unlock(&serial->shared_int->shared_int_lock);
  2331. /* setup the proc dirs and files if needed */
  2332. hso_log_port(hso_dev);
  2333. /* done, return it */
  2334. return hso_dev;
  2335. err_free_serial:
  2336. kfree(serial);
  2337. err_free_dev:
  2338. kfree(hso_dev);
  2339. return NULL;
  2340. }
  2341. static void hso_free_shared_int(struct hso_shared_int *mux)
  2342. {
  2343. usb_free_urb(mux->shared_intr_urb);
  2344. kfree(mux->shared_intr_buf);
  2345. mutex_unlock(&mux->shared_int_lock);
  2346. kfree(mux);
  2347. }
  2348. static
  2349. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2350. {
  2351. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2352. if (!mux)
  2353. return NULL;
  2354. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2355. USB_DIR_IN);
  2356. if (!mux->intr_endp) {
  2357. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2358. goto exit;
  2359. }
  2360. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2361. if (!mux->shared_intr_urb)
  2362. goto exit;
  2363. mux->shared_intr_buf =
  2364. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2365. GFP_KERNEL);
  2366. if (!mux->shared_intr_buf)
  2367. goto exit;
  2368. mutex_init(&mux->shared_int_lock);
  2369. return mux;
  2370. exit:
  2371. kfree(mux->shared_intr_buf);
  2372. usb_free_urb(mux->shared_intr_urb);
  2373. kfree(mux);
  2374. return NULL;
  2375. }
  2376. /* Gets the port spec for a certain interface */
  2377. static int hso_get_config_data(struct usb_interface *interface)
  2378. {
  2379. struct usb_device *usbdev = interface_to_usbdev(interface);
  2380. u8 *config_data = kmalloc(17, GFP_KERNEL);
  2381. u32 if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2382. s32 result;
  2383. if (!config_data)
  2384. return -ENOMEM;
  2385. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2386. 0x86, 0xC0, 0, 0, config_data, 17,
  2387. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2388. kfree(config_data);
  2389. return -EIO;
  2390. }
  2391. /* check if we have a valid interface */
  2392. if (if_num > 16) {
  2393. kfree(config_data);
  2394. return -EINVAL;
  2395. }
  2396. switch (config_data[if_num]) {
  2397. case 0x0:
  2398. result = 0;
  2399. break;
  2400. case 0x1:
  2401. result = HSO_PORT_DIAG;
  2402. break;
  2403. case 0x2:
  2404. result = HSO_PORT_GPS;
  2405. break;
  2406. case 0x3:
  2407. result = HSO_PORT_GPS_CONTROL;
  2408. break;
  2409. case 0x4:
  2410. result = HSO_PORT_APP;
  2411. break;
  2412. case 0x5:
  2413. result = HSO_PORT_APP2;
  2414. break;
  2415. case 0x6:
  2416. result = HSO_PORT_CONTROL;
  2417. break;
  2418. case 0x7:
  2419. result = HSO_PORT_NETWORK;
  2420. break;
  2421. case 0x8:
  2422. result = HSO_PORT_MODEM;
  2423. break;
  2424. case 0x9:
  2425. result = HSO_PORT_MSD;
  2426. break;
  2427. case 0xa:
  2428. result = HSO_PORT_PCSC;
  2429. break;
  2430. case 0xb:
  2431. result = HSO_PORT_VOICE;
  2432. break;
  2433. default:
  2434. result = 0;
  2435. }
  2436. if (result)
  2437. result |= HSO_INTF_BULK;
  2438. if (config_data[16] & 0x1)
  2439. result |= HSO_INFO_CRC_BUG;
  2440. kfree(config_data);
  2441. return result;
  2442. }
  2443. /* called once for each interface upon device insertion */
  2444. static int hso_probe(struct usb_interface *interface,
  2445. const struct usb_device_id *id)
  2446. {
  2447. int mux, i, if_num, port_spec;
  2448. unsigned char port_mask;
  2449. struct hso_device *hso_dev = NULL;
  2450. struct hso_shared_int *shared_int;
  2451. struct hso_device *tmp_dev = NULL;
  2452. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2453. dev_err(&interface->dev, "Not our interface\n");
  2454. return -ENODEV;
  2455. }
  2456. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2457. /* Get the interface/port specification from either driver_info or from
  2458. * the device itself */
  2459. if (id->driver_info) {
  2460. /* if_num is controlled by the device, driver_info is a 0 terminated
  2461. * array. Make sure, the access is in bounds! */
  2462. for (i = 0; i <= if_num; ++i)
  2463. if (((u32 *)(id->driver_info))[i] == 0)
  2464. goto exit;
  2465. port_spec = ((u32 *)(id->driver_info))[if_num];
  2466. } else {
  2467. port_spec = hso_get_config_data(interface);
  2468. if (port_spec < 0)
  2469. goto exit;
  2470. }
  2471. /* Check if we need to switch to alt interfaces prior to port
  2472. * configuration */
  2473. if (interface->num_altsetting > 1)
  2474. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2475. interface->needs_remote_wakeup = 1;
  2476. /* Allocate new hso device(s) */
  2477. switch (port_spec & HSO_INTF_MASK) {
  2478. case HSO_INTF_MUX:
  2479. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2480. /* Create the network device */
  2481. if (!disable_net) {
  2482. hso_dev = hso_create_net_device(interface,
  2483. port_spec);
  2484. if (!hso_dev)
  2485. goto exit;
  2486. tmp_dev = hso_dev;
  2487. }
  2488. }
  2489. if (hso_get_mux_ports(interface, &port_mask))
  2490. /* TODO: de-allocate everything */
  2491. goto exit;
  2492. shared_int = hso_create_shared_int(interface);
  2493. if (!shared_int)
  2494. goto exit;
  2495. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2496. if (port_mask & i) {
  2497. hso_dev = hso_create_mux_serial_device(
  2498. interface, i, shared_int);
  2499. if (!hso_dev)
  2500. goto exit;
  2501. }
  2502. }
  2503. if (tmp_dev)
  2504. hso_dev = tmp_dev;
  2505. break;
  2506. case HSO_INTF_BULK:
  2507. /* It's a regular bulk interface */
  2508. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2509. if (!disable_net)
  2510. hso_dev =
  2511. hso_create_net_device(interface, port_spec);
  2512. } else {
  2513. hso_dev =
  2514. hso_create_bulk_serial_device(interface, port_spec);
  2515. }
  2516. if (!hso_dev)
  2517. goto exit;
  2518. break;
  2519. default:
  2520. goto exit;
  2521. }
  2522. /* save our data pointer in this device */
  2523. usb_set_intfdata(interface, hso_dev);
  2524. /* done */
  2525. return 0;
  2526. exit:
  2527. hso_free_interface(interface);
  2528. return -ENODEV;
  2529. }
  2530. /* device removed, cleaning up */
  2531. static void hso_disconnect(struct usb_interface *interface)
  2532. {
  2533. hso_free_interface(interface);
  2534. /* remove reference of our private data */
  2535. usb_set_intfdata(interface, NULL);
  2536. }
  2537. static void async_get_intf(struct work_struct *data)
  2538. {
  2539. struct hso_device *hso_dev =
  2540. container_of(data, struct hso_device, async_get_intf);
  2541. usb_autopm_get_interface(hso_dev->interface);
  2542. }
  2543. static void async_put_intf(struct work_struct *data)
  2544. {
  2545. struct hso_device *hso_dev =
  2546. container_of(data, struct hso_device, async_put_intf);
  2547. usb_autopm_put_interface(hso_dev->interface);
  2548. }
  2549. static int hso_get_activity(struct hso_device *hso_dev)
  2550. {
  2551. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2552. if (!hso_dev->is_active) {
  2553. hso_dev->is_active = 1;
  2554. schedule_work(&hso_dev->async_get_intf);
  2555. }
  2556. }
  2557. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2558. return -EAGAIN;
  2559. usb_mark_last_busy(hso_dev->usb);
  2560. return 0;
  2561. }
  2562. static int hso_put_activity(struct hso_device *hso_dev)
  2563. {
  2564. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2565. if (hso_dev->is_active) {
  2566. hso_dev->is_active = 0;
  2567. schedule_work(&hso_dev->async_put_intf);
  2568. return -EAGAIN;
  2569. }
  2570. }
  2571. hso_dev->is_active = 0;
  2572. return 0;
  2573. }
  2574. /* called by kernel when we need to suspend device */
  2575. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2576. {
  2577. int i, result;
  2578. /* Stop all serial ports */
  2579. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2580. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2581. result = hso_stop_serial_device(serial_table[i]);
  2582. if (result)
  2583. goto out;
  2584. }
  2585. }
  2586. /* Stop all network ports */
  2587. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2588. if (network_table[i] &&
  2589. (network_table[i]->interface == iface)) {
  2590. result = hso_stop_net_device(network_table[i]);
  2591. if (result)
  2592. goto out;
  2593. }
  2594. }
  2595. out:
  2596. return 0;
  2597. }
  2598. /* called by kernel when we need to resume device */
  2599. static int hso_resume(struct usb_interface *iface)
  2600. {
  2601. int i, result = 0;
  2602. struct hso_net *hso_net;
  2603. /* Start all serial ports */
  2604. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2605. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2606. if (dev2ser(serial_table[i])->port.count) {
  2607. result =
  2608. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2609. hso_kick_transmit(dev2ser(serial_table[i]));
  2610. if (result)
  2611. goto out;
  2612. }
  2613. }
  2614. }
  2615. /* Start all network ports */
  2616. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2617. if (network_table[i] &&
  2618. (network_table[i]->interface == iface)) {
  2619. hso_net = dev2net(network_table[i]);
  2620. if (hso_net->flags & IFF_UP) {
  2621. /* First transmit any lingering data,
  2622. then restart the device. */
  2623. if (hso_net->skb_tx_buf) {
  2624. dev_dbg(&iface->dev,
  2625. "Transmitting"
  2626. " lingering data\n");
  2627. hso_net_start_xmit(hso_net->skb_tx_buf,
  2628. hso_net->net);
  2629. hso_net->skb_tx_buf = NULL;
  2630. }
  2631. result = hso_start_net_device(network_table[i]);
  2632. if (result)
  2633. goto out;
  2634. }
  2635. }
  2636. }
  2637. out:
  2638. return result;
  2639. }
  2640. static void hso_serial_ref_free(struct kref *ref)
  2641. {
  2642. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2643. hso_free_serial_device(hso_dev);
  2644. }
  2645. static void hso_free_interface(struct usb_interface *interface)
  2646. {
  2647. struct hso_serial *serial;
  2648. int i;
  2649. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2650. if (serial_table[i] &&
  2651. (serial_table[i]->interface == interface)) {
  2652. serial = dev2ser(serial_table[i]);
  2653. tty_port_tty_hangup(&serial->port, false);
  2654. mutex_lock(&serial->parent->mutex);
  2655. serial->parent->usb_gone = 1;
  2656. mutex_unlock(&serial->parent->mutex);
  2657. cancel_work_sync(&serial_table[i]->async_put_intf);
  2658. cancel_work_sync(&serial_table[i]->async_get_intf);
  2659. hso_serial_tty_unregister(serial);
  2660. kref_put(&serial->parent->ref, hso_serial_ref_free);
  2661. }
  2662. }
  2663. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2664. if (network_table[i] &&
  2665. (network_table[i]->interface == interface)) {
  2666. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2667. /* hso_stop_net_device doesn't stop the net queue since
  2668. * traffic needs to start it again when suspended */
  2669. netif_stop_queue(dev2net(network_table[i])->net);
  2670. hso_stop_net_device(network_table[i]);
  2671. cancel_work_sync(&network_table[i]->async_put_intf);
  2672. cancel_work_sync(&network_table[i]->async_get_intf);
  2673. if (rfk) {
  2674. rfkill_unregister(rfk);
  2675. rfkill_destroy(rfk);
  2676. }
  2677. hso_free_net_device(network_table[i]);
  2678. }
  2679. }
  2680. }
  2681. /* Helper functions */
  2682. /* Get the endpoint ! */
  2683. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2684. int type, int dir)
  2685. {
  2686. int i;
  2687. struct usb_host_interface *iface = intf->cur_altsetting;
  2688. struct usb_endpoint_descriptor *endp;
  2689. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2690. endp = &iface->endpoint[i].desc;
  2691. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2692. (usb_endpoint_type(endp) == type))
  2693. return endp;
  2694. }
  2695. return NULL;
  2696. }
  2697. /* Get the byte that describes which ports are enabled */
  2698. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2699. {
  2700. int i;
  2701. struct usb_host_interface *iface = intf->cur_altsetting;
  2702. if (iface->extralen == 3) {
  2703. *ports = iface->extra[2];
  2704. return 0;
  2705. }
  2706. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2707. if (iface->endpoint[i].extralen == 3) {
  2708. *ports = iface->endpoint[i].extra[2];
  2709. return 0;
  2710. }
  2711. }
  2712. return -1;
  2713. }
  2714. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2715. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2716. struct usb_device *usb, gfp_t gfp)
  2717. {
  2718. int result;
  2719. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2720. usb_rcvintpipe(usb,
  2721. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2722. shared_int->shared_intr_buf,
  2723. 1,
  2724. intr_callback, shared_int,
  2725. shared_int->intr_endp->bInterval);
  2726. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2727. if (result)
  2728. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2729. result);
  2730. return result;
  2731. }
  2732. /* operations setup of the serial interface */
  2733. static const struct tty_operations hso_serial_ops = {
  2734. .open = hso_serial_open,
  2735. .close = hso_serial_close,
  2736. .write = hso_serial_write,
  2737. .write_room = hso_serial_write_room,
  2738. .cleanup = hso_serial_cleanup,
  2739. .ioctl = hso_serial_ioctl,
  2740. .set_termios = hso_serial_set_termios,
  2741. .chars_in_buffer = hso_serial_chars_in_buffer,
  2742. .tiocmget = hso_serial_tiocmget,
  2743. .tiocmset = hso_serial_tiocmset,
  2744. .get_icount = hso_get_count,
  2745. .unthrottle = hso_unthrottle
  2746. };
  2747. static struct usb_driver hso_driver = {
  2748. .name = driver_name,
  2749. .probe = hso_probe,
  2750. .disconnect = hso_disconnect,
  2751. .id_table = hso_ids,
  2752. .suspend = hso_suspend,
  2753. .resume = hso_resume,
  2754. .reset_resume = hso_resume,
  2755. .supports_autosuspend = 1,
  2756. .disable_hub_initiated_lpm = 1,
  2757. };
  2758. static int __init hso_init(void)
  2759. {
  2760. int i;
  2761. int result;
  2762. /* put it in the log */
  2763. pr_info("%s\n", version);
  2764. /* Initialise the serial table semaphore and table */
  2765. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2766. serial_table[i] = NULL;
  2767. /* allocate our driver using the proper amount of supported minors */
  2768. tty_drv = tty_alloc_driver(HSO_SERIAL_TTY_MINORS, TTY_DRIVER_REAL_RAW |
  2769. TTY_DRIVER_DYNAMIC_DEV);
  2770. if (IS_ERR(tty_drv))
  2771. return PTR_ERR(tty_drv);
  2772. /* fill in all needed values */
  2773. tty_drv->driver_name = driver_name;
  2774. tty_drv->name = tty_filename;
  2775. /* if major number is provided as parameter, use that one */
  2776. if (tty_major)
  2777. tty_drv->major = tty_major;
  2778. tty_drv->minor_start = 0;
  2779. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2780. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2781. tty_drv->init_termios = tty_std_termios;
  2782. hso_init_termios(&tty_drv->init_termios);
  2783. tty_set_operations(tty_drv, &hso_serial_ops);
  2784. /* register the tty driver */
  2785. result = tty_register_driver(tty_drv);
  2786. if (result) {
  2787. pr_err("%s - tty_register_driver failed(%d)\n",
  2788. __func__, result);
  2789. goto err_free_tty;
  2790. }
  2791. /* register this module as an usb driver */
  2792. result = usb_register(&hso_driver);
  2793. if (result) {
  2794. pr_err("Could not register hso driver - error: %d\n", result);
  2795. goto err_unreg_tty;
  2796. }
  2797. /* done */
  2798. return 0;
  2799. err_unreg_tty:
  2800. tty_unregister_driver(tty_drv);
  2801. err_free_tty:
  2802. tty_driver_kref_put(tty_drv);
  2803. return result;
  2804. }
  2805. static void __exit hso_exit(void)
  2806. {
  2807. pr_info("unloaded\n");
  2808. tty_unregister_driver(tty_drv);
  2809. /* deregister the usb driver */
  2810. usb_deregister(&hso_driver);
  2811. tty_driver_kref_put(tty_drv);
  2812. }
  2813. /* Module definitions */
  2814. module_init(hso_init);
  2815. module_exit(hso_exit);
  2816. MODULE_AUTHOR(MOD_AUTHOR);
  2817. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2818. MODULE_LICENSE("GPL");
  2819. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2820. MODULE_PARM_DESC(debug, "debug level mask [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2821. module_param(debug, int, 0644);
  2822. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2823. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2824. module_param(tty_major, int, 0644);
  2825. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2826. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2827. module_param(disable_net, int, 0644);