hso.c 84 KB

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