flexcop-usb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
  4. * flexcop-usb.c - covers the USB part
  5. * see flexcop.c for copyright information
  6. */
  7. #define FC_LOG_PREFIX "flexcop_usb"
  8. #include "flexcop-usb.h"
  9. #include "flexcop-common.h"
  10. /* Version information */
  11. #define DRIVER_VERSION "0.1"
  12. #define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV USB Driver"
  13. #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
  14. /* debug */
  15. #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
  16. #define dprintk(level, args...) \
  17. do { if ((debug & (level))) printk(args); } while (0)
  18. #define debug_dump(b, l, method) do {\
  19. int i; \
  20. for (i = 0; i < l; i++) \
  21. method("%02x ", b[i]); \
  22. method("\n"); \
  23. } while (0)
  24. #define DEBSTATUS ""
  25. #else
  26. #define dprintk(level, args...) no_printk(args)
  27. #define debug_dump(b, l, method) do { } while (0)
  28. #define DEBSTATUS " (debugging is not enabled)"
  29. #endif
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "set debugging level (1=info,ts=2,ctrl=4,i2c=8,v8mem=16 (or-able))." DEBSTATUS);
  33. #undef DEBSTATUS
  34. #define deb_info(args...) dprintk(0x01, args)
  35. #define deb_ts(args...) dprintk(0x02, args)
  36. #define deb_ctrl(args...) dprintk(0x04, args)
  37. #define deb_i2c(args...) dprintk(0x08, args)
  38. #define deb_v8(args...) dprintk(0x10, args)
  39. /* JLP 111700: we will include the 1 bit gap between the upper and lower 3 bits
  40. * in the IBI address, to make the V8 code simpler.
  41. * PCI ADDRESS FORMAT: 0x71C -> 0000 0111 0001 1100 (the six bits used)
  42. * in general: 0000 0HHH 000L LL00
  43. * IBI ADDRESS FORMAT: RHHH BLLL
  44. *
  45. * where R is the read(1)/write(0) bit, B is the busy bit
  46. * and HHH and LLL are the two sets of three bits from the PCI address.
  47. */
  48. #define B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(usPCI) (u8) \
  49. (((usPCI >> 2) & 0x07) + ((usPCI >> 4) & 0x70))
  50. #define B2C2_FLEX_INTERNALADDR_TO_PCIOFFSET(ucAddr) (u16) \
  51. (((ucAddr & 0x07) << 2) + ((ucAddr & 0x70) << 4))
  52. /*
  53. * DKT 020228
  54. * - forget about this VENDOR_BUFFER_SIZE, read and write register
  55. * deal with DWORD or 4 bytes, that should be should from now on
  56. * - from now on, we don't support anything older than firm 1.00
  57. * I eliminated the write register as a 2 trip of writing hi word and lo word
  58. * and force this to write only 4 bytes at a time.
  59. * NOTE: this should work with all the firmware from 1.00 and newer
  60. */
  61. static int flexcop_usb_readwrite_dw(struct flexcop_device *fc, u16 wRegOffsPCI, u32 *val, u8 read)
  62. {
  63. struct flexcop_usb *fc_usb = fc->bus_specific;
  64. u8 request = read ? B2C2_USB_READ_REG : B2C2_USB_WRITE_REG;
  65. u8 request_type = (read ? USB_DIR_IN : USB_DIR_OUT) | USB_TYPE_VENDOR;
  66. u8 wAddress = B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(wRegOffsPCI) |
  67. (read ? 0x80 : 0);
  68. int ret;
  69. mutex_lock(&fc_usb->data_mutex);
  70. if (!read)
  71. memcpy(fc_usb->data, val, sizeof(*val));
  72. ret = usb_control_msg(fc_usb->udev,
  73. read ? B2C2_USB_CTRL_PIPE_IN : B2C2_USB_CTRL_PIPE_OUT,
  74. request,
  75. request_type, /* 0xc0 read or 0x40 write */
  76. wAddress,
  77. 0,
  78. fc_usb->data,
  79. sizeof(u32),
  80. B2C2_WAIT_FOR_OPERATION_RDW);
  81. if (ret != sizeof(u32)) {
  82. err("error while %s dword from %d (%d).", read ? "reading" :
  83. "writing", wAddress, wRegOffsPCI);
  84. if (ret >= 0)
  85. ret = -EIO;
  86. }
  87. if (read && ret >= 0)
  88. memcpy(val, fc_usb->data, sizeof(*val));
  89. mutex_unlock(&fc_usb->data_mutex);
  90. return ret;
  91. }
  92. /*
  93. * DKT 010817 - add support for V8 memory read/write and flash update
  94. */
  95. static int flexcop_usb_v8_memory_req(struct flexcop_usb *fc_usb,
  96. flexcop_usb_request_t req, u8 page, u16 wAddress,
  97. u8 *pbBuffer, u32 buflen)
  98. {
  99. u8 request_type = USB_TYPE_VENDOR;
  100. u16 wIndex;
  101. int nWaitTime, pipe, ret;
  102. wIndex = page << 8;
  103. if (buflen > sizeof(fc_usb->data)) {
  104. err("Buffer size bigger than max URB control message\n");
  105. return -EIO;
  106. }
  107. switch (req) {
  108. case B2C2_USB_READ_V8_MEM:
  109. nWaitTime = B2C2_WAIT_FOR_OPERATION_V8READ;
  110. request_type |= USB_DIR_IN;
  111. pipe = B2C2_USB_CTRL_PIPE_IN;
  112. break;
  113. case B2C2_USB_WRITE_V8_MEM:
  114. wIndex |= pbBuffer[0];
  115. request_type |= USB_DIR_OUT;
  116. nWaitTime = B2C2_WAIT_FOR_OPERATION_V8WRITE;
  117. pipe = B2C2_USB_CTRL_PIPE_OUT;
  118. break;
  119. case B2C2_USB_FLASH_BLOCK:
  120. request_type |= USB_DIR_OUT;
  121. nWaitTime = B2C2_WAIT_FOR_OPERATION_V8FLASH;
  122. pipe = B2C2_USB_CTRL_PIPE_OUT;
  123. break;
  124. default:
  125. deb_info("unsupported request for v8_mem_req %x.\n", req);
  126. return -EINVAL;
  127. }
  128. deb_v8("v8mem: %02x %02x %04x %04x, len: %d\n", request_type, req,
  129. wAddress, wIndex, buflen);
  130. mutex_lock(&fc_usb->data_mutex);
  131. if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
  132. memcpy(fc_usb->data, pbBuffer, buflen);
  133. ret = usb_control_msg(fc_usb->udev, pipe,
  134. req,
  135. request_type,
  136. wAddress,
  137. wIndex,
  138. fc_usb->data,
  139. buflen,
  140. nWaitTime);
  141. if (ret != buflen)
  142. ret = -EIO;
  143. if (ret >= 0) {
  144. ret = 0;
  145. if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  146. memcpy(pbBuffer, fc_usb->data, buflen);
  147. }
  148. mutex_unlock(&fc_usb->data_mutex);
  149. debug_dump(pbBuffer, ret, deb_v8);
  150. return ret;
  151. }
  152. #define bytes_left_to_read_on_page(paddr, buflen) \
  153. ((V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)) > buflen \
  154. ? buflen : (V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)))
  155. static int flexcop_usb_memory_req(struct flexcop_usb *fc_usb,
  156. flexcop_usb_request_t req, flexcop_usb_mem_page_t page_start,
  157. u32 addr, int extended, u8 *buf, u32 len)
  158. {
  159. int ret = 0;
  160. u16 wMax;
  161. u32 pagechunk = 0;
  162. switch (req) {
  163. case B2C2_USB_READ_V8_MEM:
  164. wMax = USB_MEM_READ_MAX;
  165. break;
  166. case B2C2_USB_WRITE_V8_MEM:
  167. wMax = USB_MEM_WRITE_MAX;
  168. break;
  169. case B2C2_USB_FLASH_BLOCK:
  170. wMax = USB_FLASH_MAX;
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. while (len) {
  176. pagechunk = min(wMax, bytes_left_to_read_on_page(addr, len));
  177. deb_info("%x\n",
  178. (addr & V8_MEMORY_PAGE_MASK) |
  179. (V8_MEMORY_EXTENDED*extended));
  180. ret = flexcop_usb_v8_memory_req(fc_usb, req,
  181. page_start + (addr / V8_MEMORY_PAGE_SIZE),
  182. (addr & V8_MEMORY_PAGE_MASK) |
  183. (V8_MEMORY_EXTENDED*extended),
  184. buf, pagechunk);
  185. if (ret < 0)
  186. return ret;
  187. addr += pagechunk;
  188. buf += pagechunk;
  189. len -= pagechunk;
  190. }
  191. return 0;
  192. }
  193. static int flexcop_usb_get_mac_addr(struct flexcop_device *fc, int extended)
  194. {
  195. return flexcop_usb_memory_req(fc->bus_specific, B2C2_USB_READ_V8_MEM,
  196. V8_MEMORY_PAGE_FLASH, 0x1f010, 1,
  197. fc->dvb_adapter.proposed_mac, 6);
  198. }
  199. /* usb i2c stuff */
  200. static int flexcop_usb_i2c_req(struct flexcop_i2c_adapter *i2c,
  201. flexcop_usb_request_t req, flexcop_usb_i2c_function_t func,
  202. u8 chipaddr, u8 addr, u8 *buf, u8 buflen)
  203. {
  204. struct flexcop_usb *fc_usb = i2c->fc->bus_specific;
  205. u16 wValue, wIndex;
  206. int nWaitTime, pipe, ret;
  207. u8 request_type = USB_TYPE_VENDOR;
  208. if (buflen > sizeof(fc_usb->data)) {
  209. err("Buffer size bigger than max URB control message\n");
  210. return -EIO;
  211. }
  212. switch (func) {
  213. case USB_FUNC_I2C_WRITE:
  214. case USB_FUNC_I2C_MULTIWRITE:
  215. case USB_FUNC_I2C_REPEATWRITE:
  216. /* DKT 020208 - add this to support special case of DiSEqC */
  217. case USB_FUNC_I2C_CHECKWRITE:
  218. pipe = B2C2_USB_CTRL_PIPE_OUT;
  219. nWaitTime = 2000;
  220. request_type |= USB_DIR_OUT;
  221. break;
  222. case USB_FUNC_I2C_READ:
  223. case USB_FUNC_I2C_REPEATREAD:
  224. pipe = B2C2_USB_CTRL_PIPE_IN;
  225. nWaitTime = 2000;
  226. request_type |= USB_DIR_IN;
  227. break;
  228. default:
  229. deb_info("unsupported function for i2c_req %x\n", func);
  230. return -EINVAL;
  231. }
  232. wValue = (func << 8) | (i2c->port << 4);
  233. wIndex = (chipaddr << 8 ) | addr;
  234. deb_i2c("i2c %2d: %02x %02x %02x %02x %02x %02x\n",
  235. func, request_type, req,
  236. wValue & 0xff, wValue >> 8,
  237. wIndex & 0xff, wIndex >> 8);
  238. mutex_lock(&fc_usb->data_mutex);
  239. if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
  240. memcpy(fc_usb->data, buf, buflen);
  241. ret = usb_control_msg(fc_usb->udev, pipe,
  242. req,
  243. request_type,
  244. wValue,
  245. wIndex,
  246. fc_usb->data,
  247. buflen,
  248. nWaitTime);
  249. if (ret != buflen)
  250. ret = -EIO;
  251. if (ret >= 0) {
  252. ret = 0;
  253. if ((request_type & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  254. memcpy(buf, fc_usb->data, buflen);
  255. }
  256. mutex_unlock(&fc_usb->data_mutex);
  257. return ret;
  258. }
  259. /* actual bus specific access functions,
  260. make sure prototype are/will be equal to pci */
  261. static flexcop_ibi_value flexcop_usb_read_ibi_reg(struct flexcop_device *fc,
  262. flexcop_ibi_register reg)
  263. {
  264. flexcop_ibi_value val;
  265. val.raw = 0;
  266. flexcop_usb_readwrite_dw(fc, reg, &val.raw, 1);
  267. return val;
  268. }
  269. static int flexcop_usb_write_ibi_reg(struct flexcop_device *fc,
  270. flexcop_ibi_register reg, flexcop_ibi_value val)
  271. {
  272. return flexcop_usb_readwrite_dw(fc, reg, &val.raw, 0);
  273. }
  274. static int flexcop_usb_i2c_request(struct flexcop_i2c_adapter *i2c,
  275. flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
  276. {
  277. if (op == FC_READ)
  278. return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST,
  279. USB_FUNC_I2C_READ, chipaddr, addr, buf, len);
  280. else
  281. return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST,
  282. USB_FUNC_I2C_WRITE, chipaddr, addr, buf, len);
  283. }
  284. static void flexcop_usb_process_frame(struct flexcop_usb *fc_usb,
  285. u8 *buffer, int buffer_length)
  286. {
  287. u8 *b;
  288. int l;
  289. deb_ts("tmp_buffer_length=%d, buffer_length=%d\n",
  290. fc_usb->tmp_buffer_length, buffer_length);
  291. if (fc_usb->tmp_buffer_length > 0) {
  292. memcpy(fc_usb->tmp_buffer+fc_usb->tmp_buffer_length, buffer,
  293. buffer_length);
  294. fc_usb->tmp_buffer_length += buffer_length;
  295. b = fc_usb->tmp_buffer;
  296. l = fc_usb->tmp_buffer_length;
  297. } else {
  298. b = buffer;
  299. l = buffer_length;
  300. }
  301. while (l >= 190) {
  302. if (*b == 0xff) {
  303. switch (*(b+1) & 0x03) {
  304. case 0x01: /* media packet */
  305. if (*(b+2) == 0x47)
  306. flexcop_pass_dmx_packets(
  307. fc_usb->fc_dev, b+2, 1);
  308. else
  309. deb_ts("not ts packet %*ph\n", 4, b+2);
  310. b += 190;
  311. l -= 190;
  312. break;
  313. default:
  314. deb_ts("wrong packet type\n");
  315. l = 0;
  316. break;
  317. }
  318. } else {
  319. deb_ts("wrong header\n");
  320. l = 0;
  321. }
  322. }
  323. if (l > 0)
  324. memcpy(fc_usb->tmp_buffer, b, l);
  325. fc_usb->tmp_buffer_length = l;
  326. }
  327. static void flexcop_usb_urb_complete(struct urb *urb)
  328. {
  329. struct flexcop_usb *fc_usb = urb->context;
  330. int i;
  331. if (urb->actual_length > 0)
  332. deb_ts("urb completed, bufsize: %d actlen; %d\n",
  333. urb->transfer_buffer_length, urb->actual_length);
  334. for (i = 0; i < urb->number_of_packets; i++) {
  335. if (urb->iso_frame_desc[i].status < 0) {
  336. err("iso frame descriptor %d has an error: %d\n", i,
  337. urb->iso_frame_desc[i].status);
  338. } else
  339. if (urb->iso_frame_desc[i].actual_length > 0) {
  340. deb_ts("passed %d bytes to the demux\n",
  341. urb->iso_frame_desc[i].actual_length);
  342. flexcop_usb_process_frame(fc_usb,
  343. urb->transfer_buffer +
  344. urb->iso_frame_desc[i].offset,
  345. urb->iso_frame_desc[i].actual_length);
  346. }
  347. urb->iso_frame_desc[i].status = 0;
  348. urb->iso_frame_desc[i].actual_length = 0;
  349. }
  350. usb_submit_urb(urb, GFP_ATOMIC);
  351. }
  352. static int flexcop_usb_stream_control(struct flexcop_device *fc, int onoff)
  353. {
  354. /* submit/kill iso packets */
  355. return 0;
  356. }
  357. static void flexcop_usb_transfer_exit(struct flexcop_usb *fc_usb)
  358. {
  359. int i;
  360. for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++)
  361. if (fc_usb->iso_urb[i] != NULL) {
  362. deb_ts("unlinking/killing urb no. %d\n", i);
  363. usb_kill_urb(fc_usb->iso_urb[i]);
  364. usb_free_urb(fc_usb->iso_urb[i]);
  365. }
  366. usb_free_coherent(fc_usb->udev, fc_usb->buffer_size,
  367. fc_usb->iso_buffer, fc_usb->dma_addr);
  368. }
  369. static int flexcop_usb_transfer_init(struct flexcop_usb *fc_usb)
  370. {
  371. struct usb_host_interface *alt = fc_usb->uintf->cur_altsetting;
  372. u16 frame_size;
  373. int bufsize, i, j, ret;
  374. int buffer_offset = 0;
  375. frame_size = usb_endpoint_maxp(&alt->endpoint[0].desc);
  376. bufsize = B2C2_USB_NUM_ISO_URB * B2C2_USB_FRAMES_PER_ISO * frame_size;
  377. deb_ts("creating %d iso-urbs with %d frames each of %d bytes size = %d.\n",
  378. B2C2_USB_NUM_ISO_URB,
  379. B2C2_USB_FRAMES_PER_ISO, frame_size, bufsize);
  380. fc_usb->iso_buffer = usb_alloc_coherent(fc_usb->udev,
  381. bufsize, GFP_KERNEL, &fc_usb->dma_addr);
  382. if (fc_usb->iso_buffer == NULL)
  383. return -ENOMEM;
  384. memset(fc_usb->iso_buffer, 0, bufsize);
  385. fc_usb->buffer_size = bufsize;
  386. /* creating iso urbs */
  387. for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) {
  388. fc_usb->iso_urb[i] = usb_alloc_urb(B2C2_USB_FRAMES_PER_ISO,
  389. GFP_KERNEL);
  390. if (fc_usb->iso_urb[i] == NULL) {
  391. ret = -ENOMEM;
  392. goto urb_error;
  393. }
  394. }
  395. /* initialising and submitting iso urbs */
  396. for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) {
  397. int frame_offset = 0;
  398. struct urb *urb = fc_usb->iso_urb[i];
  399. deb_ts("initializing and submitting urb no. %d (buf_offset: %d).\n",
  400. i, buffer_offset);
  401. urb->dev = fc_usb->udev;
  402. urb->context = fc_usb;
  403. urb->complete = flexcop_usb_urb_complete;
  404. urb->pipe = B2C2_USB_DATA_PIPE;
  405. urb->transfer_flags = URB_ISO_ASAP;
  406. urb->interval = 1;
  407. urb->number_of_packets = B2C2_USB_FRAMES_PER_ISO;
  408. urb->transfer_buffer_length = frame_size * B2C2_USB_FRAMES_PER_ISO;
  409. urb->transfer_buffer = fc_usb->iso_buffer + buffer_offset;
  410. buffer_offset += frame_size * B2C2_USB_FRAMES_PER_ISO;
  411. for (j = 0; j < B2C2_USB_FRAMES_PER_ISO; j++) {
  412. deb_ts("urb no: %d, frame: %d, frame_offset: %d\n",
  413. i, j, frame_offset);
  414. urb->iso_frame_desc[j].offset = frame_offset;
  415. urb->iso_frame_desc[j].length = frame_size;
  416. frame_offset += frame_size;
  417. }
  418. if ((ret = usb_submit_urb(fc_usb->iso_urb[i],GFP_KERNEL))) {
  419. err("submitting urb %d failed with %d.", i, ret);
  420. goto urb_error;
  421. }
  422. deb_ts("submitted urb no. %d.\n", i);
  423. }
  424. /* SRAM */
  425. flexcop_sram_set_dest(fc_usb->fc_dev, FC_SRAM_DEST_MEDIA |
  426. FC_SRAM_DEST_NET | FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI,
  427. FC_SRAM_DEST_TARGET_WAN_USB);
  428. flexcop_wan_set_speed(fc_usb->fc_dev, FC_WAN_SPEED_8MBITS);
  429. flexcop_sram_ctrl(fc_usb->fc_dev, 1, 1, 1);
  430. return 0;
  431. urb_error:
  432. flexcop_usb_transfer_exit(fc_usb);
  433. return ret;
  434. }
  435. static int flexcop_usb_init(struct flexcop_usb *fc_usb)
  436. {
  437. struct usb_host_interface *alt;
  438. int ret;
  439. /* use the alternate setting with the largest buffer */
  440. ret = usb_set_interface(fc_usb->udev, 0, 1);
  441. if (ret) {
  442. err("set interface failed.");
  443. return ret;
  444. }
  445. alt = fc_usb->uintf->cur_altsetting;
  446. if (alt->desc.bNumEndpoints < 2)
  447. return -ENODEV;
  448. if (!usb_endpoint_is_isoc_in(&alt->endpoint[0].desc))
  449. return -ENODEV;
  450. switch (fc_usb->udev->speed) {
  451. case USB_SPEED_LOW:
  452. err("cannot handle USB speed because it is too slow.");
  453. return -ENODEV;
  454. break;
  455. case USB_SPEED_FULL:
  456. info("running at FULL speed.");
  457. break;
  458. case USB_SPEED_HIGH:
  459. info("running at HIGH speed.");
  460. break;
  461. case USB_SPEED_SUPER:
  462. info("running at SUPER speed.");
  463. break;
  464. case USB_SPEED_SUPER_PLUS:
  465. info("running at SUPER+ speed.");
  466. break;
  467. case USB_SPEED_UNKNOWN:
  468. default:
  469. err("cannot handle USB speed because it is unknown.");
  470. return -ENODEV;
  471. }
  472. usb_set_intfdata(fc_usb->uintf, fc_usb);
  473. return 0;
  474. }
  475. static void flexcop_usb_exit(struct flexcop_usb *fc_usb)
  476. {
  477. usb_set_intfdata(fc_usb->uintf, NULL);
  478. }
  479. static int flexcop_usb_probe(struct usb_interface *intf,
  480. const struct usb_device_id *id)
  481. {
  482. struct usb_device *udev = interface_to_usbdev(intf);
  483. struct flexcop_usb *fc_usb = NULL;
  484. struct flexcop_device *fc = NULL;
  485. int ret;
  486. if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_usb))) == NULL) {
  487. err("out of memory\n");
  488. return -ENOMEM;
  489. }
  490. /* general flexcop init */
  491. fc_usb = fc->bus_specific;
  492. fc_usb->fc_dev = fc;
  493. mutex_init(&fc_usb->data_mutex);
  494. fc->read_ibi_reg = flexcop_usb_read_ibi_reg;
  495. fc->write_ibi_reg = flexcop_usb_write_ibi_reg;
  496. fc->i2c_request = flexcop_usb_i2c_request;
  497. fc->get_mac_addr = flexcop_usb_get_mac_addr;
  498. fc->stream_control = flexcop_usb_stream_control;
  499. fc->pid_filtering = 1;
  500. fc->bus_type = FC_USB;
  501. fc->dev = &udev->dev;
  502. fc->owner = THIS_MODULE;
  503. /* bus specific part */
  504. fc_usb->udev = udev;
  505. fc_usb->uintf = intf;
  506. if ((ret = flexcop_usb_init(fc_usb)) != 0)
  507. goto err_kfree;
  508. /* init flexcop */
  509. if ((ret = flexcop_device_initialize(fc)) != 0)
  510. goto err_usb_exit;
  511. /* xfer init */
  512. if ((ret = flexcop_usb_transfer_init(fc_usb)) != 0)
  513. goto err_fc_exit;
  514. info("%s successfully initialized and connected.", DRIVER_NAME);
  515. return 0;
  516. err_fc_exit:
  517. flexcop_device_exit(fc);
  518. err_usb_exit:
  519. flexcop_usb_exit(fc_usb);
  520. err_kfree:
  521. flexcop_device_kfree(fc);
  522. return ret;
  523. }
  524. static void flexcop_usb_disconnect(struct usb_interface *intf)
  525. {
  526. struct flexcop_usb *fc_usb = usb_get_intfdata(intf);
  527. flexcop_usb_transfer_exit(fc_usb);
  528. flexcop_device_exit(fc_usb->fc_dev);
  529. flexcop_usb_exit(fc_usb);
  530. flexcop_device_kfree(fc_usb->fc_dev);
  531. info("%s successfully deinitialized and disconnected.", DRIVER_NAME);
  532. }
  533. static const struct usb_device_id flexcop_usb_table[] = {
  534. { USB_DEVICE(0x0af7, 0x0101) },
  535. { }
  536. };
  537. MODULE_DEVICE_TABLE (usb, flexcop_usb_table);
  538. /* usb specific object needed to register this driver with the usb subsystem */
  539. static struct usb_driver flexcop_usb_driver = {
  540. .name = "b2c2_flexcop_usb",
  541. .probe = flexcop_usb_probe,
  542. .disconnect = flexcop_usb_disconnect,
  543. .id_table = flexcop_usb_table,
  544. };
  545. module_usb_driver(flexcop_usb_driver);
  546. MODULE_AUTHOR(DRIVER_AUTHOR);
  547. MODULE_DESCRIPTION(DRIVER_NAME);
  548. MODULE_LICENSE("GPL");