hso.c 84 KB

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