hso.c 83 KB

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