hso.c 83 KB

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